16

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;
}
Edwin Toh
  • 163
  • 1
  • 2
  • 6

10 Answers10

4

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

uzair
  • 56
  • 4
2

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

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

Kousik
  • 21,485
  • 7
  • 36
  • 59
  • In this case is the solution. For me isnt because this plugin means to use some deprecated functions (I work with the 2.9) – Yises Aug 21 '13 at 09:45
2

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);
}
Jack
  • 161
  • 1
  • 13
  • 20
1

Lowering to 25 and using <preference name="android-minSdkVersion" value="7" /> helped me out!

CommanderS
  • 11
  • 1
1

Make sure that your activity tag in your AndroidManifest.xml has the following attribute:

android:configChanges="orientation|keyboardHidden"

The orientation change will cause your app to be reloaded. If you are actually getting a crash then run "adb logcat" to capture the error that is occurring.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
1

This problem isn't actually about Phonegap. It's a common issue on native android apps too.

See PhoneGap camera restarts the application

Community
  • 1
  • 1
0

For me, the trick was to modify the manifest from :

<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="14"/>

to

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

It now works but I have no idea why...

poiuytrez
  • 21,330
  • 35
  • 113
  • 172
0

I have a Samsung Galaxy Note II. I had the same problem. I changed this in the AndroidManifest.xml and now it works on Samsung and 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" />
user372225
  • 851
  • 2
  • 9
  • 23
0

DATA_URI is not recommended at times, it doesn't return to the app. Because DATA_URI gives base64 string, so some devices do not have enough memory for this. use FILE_URI only, it's safe for android device.

if you need Base64 string means you covert after call captureSuccess using File readAsDataURL

ref this link

Community
  • 1
  • 1
Kathir
  • 4,359
  • 3
  • 17
  • 29
-1

Try lowering the quality parameter to 25. Some devices don't have enough memory for 50.

Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139