Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap app quits after Camera captures image. [Android]

I'm getting an error with Phonegap 1.4.1.

The phonegap app I create restarts after I am returned to the app after taking a picture.

Here is the code I'm using:

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;
}
like image 475
Edwin Toh Avatar asked Feb 16 '12 00:02

Edwin Toh


3 Answers

I was having the same problem for 2 days until I fount that you should have access to the permission of getting the image file you took in the device. By default there is no permission of return the image so it returns null and your app crashes.

If you are using Cordova/PhoneGap in XCode then you should also create the Config.xml file in www folder and give permission to acces the image file.

<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"/>

If you are working on Android then give permission on the AndroidManifest.xml file.

you can find everything for the config.xml file here: https://build.phonegap.com/docs/config-xml

like image 145
uzair Avatar answered Nov 06 '22 13:11

uzair


use foreground-camera-plugin its will fix this issue:

https://code.google.com/p/foreground-camera-plugin/

like image 32
Nullify Avatar answered Nov 06 '22 15:11

Nullify


May be your mobile memory is not enough,This problem isn't actually about Phonegap. It's a common issue on native android apps too.It occurs because when the camera is triggered, the android activity goes background (onStop state), waiting for the camera to take the picture. Then the GC comes and kills the activity to free memory before the conclusion of camera action, and when the camera is done your activity has already died. That is why the app is restarted.

Here is my suggests

1 .Replace cramer plugin, avoid using custom plug-ins start garbage collection, (http://code.google.com/p/foreground-camera-plugin/, http://code.google.com/p/foreground-gallery- plugin /) 2. Detects memory if insufficient to kill other processes, active release memory 3. Improve their survival, try to avoid the release of memory in the system selected.

    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);
}
like image 38
Aliceljm Avatar answered Nov 06 '22 14:11

Aliceljm