Phonegap应用在相机拍摄照片后退出了。[安卓]

16

我在使用 Phonegap 1.4.1 时遇到了错误。

当我拍完照片后重新进入应用程序时,我创建的 Phonegap 应用程序会重新启动。

以下是我使用的代码:

function capturePhoto() {
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}

function onPhotoURISuccess(imageURI) {
  // Uncomment to view the image file URI 
  // console.log(imageURI);

  // Get image handle
  //
  var largeImage = document.getElementById('largeImage');

  // Unhide image elements
  //
  largeImage.style.display = 'block';

  // Show the captured photo
  // The inline CSS rules are used to resize the image
  //
  largeImage.src = imageURI;
}

遇到同样的问题 x( 降低质量没有帮助。 你能看到我的问题中崩溃日志并告诉我它们是否与你的相似吗?http://stackoverflow.com/questions/8909422/phonegap-android-app-crashing-due-to-low-memory-on-opening-camera - ghostCoder
我实际上没有任何崩溃日志。 - Edwin Toh
安装一个Logcat应用程序并从那里导出日志,然后获取崩溃日志 :P - ghostCoder
啊,我实际上有一个日志。在这里:http://d.pr/JjuQ - Edwin Toh
10个回答

4

我曾经也遇到了同样的问题,持续了两天,直到我发现你需要访问设备中拍摄的图像文件的权限。默认情况下没有返回图像的权限,因此它会返回null并导致应用程序崩溃。

如果您在XCode中使用Cordova/PhoneGap,则还应在www文件夹中创建Config.xml文件并授予访问图像文件的权限。

<feature name="http://api.phonegap.com/1.0/device" />
<feature name="http://api.phonegap.com/1.0/battery"/>
<feature name="http://api.phonegap.com/1.0/camera"/>
<feature name="http://api.phonegap.com/1.0/contacts"/>
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/geolocation"/>
<feature name="http://api.phonegap.com/1.0/media"/>
<feature name="http://api.phonegap.com/1.0/network"/>
<feature name="http://api.phonegap.com/1.0/notification"/>

如果你正在开发Android应用,需要在AndroidManifest.xml文件中授予权限。
有关config.xml文件的所有内容都可以在此处找到: https://build.phonegap.com/docs/config-xml

2
也许您的手机内存不够用了。这个问题实际上并不是针对Phonegap的,使用本地安卓应用程序也会遇到这个常见问题。当相机被触发时,安卓活动进入后台(onStop状态),等待相机拍照。然后垃圾回收(GC)会在相机动作完成之前释放内存并杀死该活动,当相机完成拍照后,您的活动已经死亡,因此应用程序被重新启动。
以下是我的建议:
1. 更换相机插件,避免使用自定义插件开始垃圾回收(http://code.google.com/p/foreground-camera-plugin/http://code.google.com/p/foreground-gallery-插件/) 2. 如果内存不足,则检测并杀死其他进程以释放内存 3. 提高生存率,尽可能避免在系统中选择释放内存。
    private void clearMemory(boolean killAll)
{
    mklog("当前系统可用内存大小是:" + getAvailMemory(getApplicationContext()));
    ActivityManager activityManger = (ActivityManager) this
            .getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> list = activityManger
            .getRunningAppProcesses();
    if (list != null)
        for (int i = 0; i < list.size(); i++)
        {
            ActivityManager.RunningAppProcessInfo apinfo = list.get(i);

            System.out.println("pid            " + apinfo.pid);
            System.out.println("processName              "
                    + apinfo.processName);
            System.out
                    .println("importance            " + apinfo.importance);
            String[] pkgList = apinfo.pkgList;

            if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND
                    || (killAll && apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE))
            {
                // Process.killProcess(apinfo.pid);
                for (int j = 0; j < pkgList.length; j++)
                {
                    activityManger.killBackgroundProcesses(pkgList[j]);
                    mklog("准备杀死进程:" + pkgList[j]);
                }
            }

        }

    mklog("清理之后 当前系统可用内存大小是:" + getAvailMemory(getApplicationContext()));
}

private long getAvailMemory(Context context)
{
    ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    am.getMemoryInfo(mi);
    return mi.availMem / (1024 * 1024);
}   
      public void mklog(String contentString)
{
    Log.i("Web Console", contentString);
}

2

在这种情况下,这是解决方案。但对我来说并不是,因为这个插件意味着要使用一些已弃用的功能(我使用的是2.9版本)。 - Yises

1
请确保您的 AndroidManifest.xml 文件中的 activity 标签具有以下属性:
android:configChanges="orientation|keyboardHidden"

屏幕方向的改变会导致您的应用程序重新加载。如果您实际上遇到了崩溃问题,请运行“adb logcat”以捕获发生的错误。


这对我解决了问题。如果文件太大,在上传时记得设置 chunkedMode="true"。 - Alexander Fradiani

1

将最低版本降至25并使用<preference name="android-minSdkVersion" value="7" />对我很有帮助!


1

谢谢你。对Phonegap的信心恢复了! - Edwin Toh

0
对我来说,诀窍是修改清单文件:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="14"/>

<uses-sdk android:minSdkVersion="7" />

现在它可以工作了,但我不知道为什么...


0

DATA_URI 有时不建议使用,因为它不能返回到应用程序。因为 DATA_URI 返回 base64 字符串,所以部分设备可能没有足够的内存。仅使用 FILE_URI,对于 Android 设备来说很安全。

如果需要 Base64 字符串,意味着您需要在调用 captureSuccess 后使用 File readAsDataURL 进行转换。

参考此链接


0

我有一部三星Galaxy Note II手机。我曾经遇到相同的问题。我在AndroidManifest.xml文件中进行了更改,现在可以在三星和HTC Thunderbolt上正常运行了。

<uses-feature android:name="android.hardware.camera" android:required="false"/>

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="10"/>

<activity  android:configChanges="orientation|keyboardHidden" />

-1

尝试将质量参数降低到25。一些设备的内存不足以支持50。


降低质量并没有起到帮助的作用。 - Edwin Toh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接