Question

I was trying to tweak the camera plugin to find what camera resolutions my device can support. Following is the CameraLauncher.java's takePicture function with my tweak in it:

/**
 * Take a picture with the camera.
 * When an image is captured or the camera view is cancelled, the result is returned
 * in CordovaActivity.onActivityResult, which forwards the result to this.onActivityResult.
 *
 * The image can either be returned as a base64 string or a URI that points to the file.
 * To display base64 string in an img tag, set the source to:
 *      img.src="data:image/jpeg;base64,"+result;
 * or to display URI in an img tag
 *      img.src=result;
 *
 * @param quality           Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
 * @param returnType        Set the type of image to return.
 */
public void takePicture(int returnType, int encodingType) {



    // **********TWEAK************* //

    camera = Camera.open(0);
    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size result = null;

    for (int i=0;i<sizes.size();i++){
        result = sizes.get(i);
        Log.i("*********PictureSize************", "Supported Size. Width: " + result.width + "height : " + result.height); 
    }


    // ********TWEAK ENDS********* //



    // Save the number of images currently on disk for later
    this.numPics = queryImgDB(whichContentStore()).getCount();

    // Display camera
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

    // Specify file so that large image is captured and returned
    File photo = createCaptureFile(encodingType);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

    this.imageUri = Uri.fromFile(photo);      

    if (this.cordova != null) {
        this.cordova.startActivityForResult((CordovaPlugin) this, intent, (CAMERA + 1) * 16 + returnType + 1);
    }
    else
        LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
}

My tweak worked. It listed all possible resolutions that my device supports. However, when the camera opens to take a picture, the camera (and my app) crashes with an alert message that says 'Camera Failed'. This message differs among different devices. For example, on a Karbonn tablet (Chinese hardware) the message says 'Unfortunately, the camera crashed'. I have no idea what's going on. When I remove my tweak, the camera works just fine.

Please help.

Here is the Logcat console:

04-30 15:57:05.815: D/dalvikvm(2598): Late-enabling CheckJNI
04-30 15:57:06.115: I/CordovaLog(2598): Changing log level to DEBUG(3)
04-30 15:57:06.115: I/CordovaLog(2598): Found start page location: index.html
04-30 15:57:06.115: D/Whitelist(2598): Unlimited access to network resources
04-30 15:57:06.125: D/CordovaActivity(2598): CordovaActivity.onCreate()
04-30 15:57:06.155: I/dalvikvm(2598): Could not find method org.apache.cordova.CordovaWebView.setWebContentsDebuggingEnabled, referenced from method org.apache.cordova.CordovaWebView.setup
04-30 15:57:06.155: W/dalvikvm(2598): VFY: unable to resolve static method 2439: Lorg/apache/cordova/CordovaWebView;.setWebContentsDebuggingEnabled (Z)V
04-30 15:57:06.155: D/dalvikvm(2598): VFY: replacing opcode 0x71 at 0x00be
04-30 15:57:06.265: D/CordovaWebView(2598): CordovaWebView is running on device made by: Karbonn
04-30 15:57:06.285: D/JsMessageQueue(2598): Set native->JS mode to 2
04-30 15:57:06.285: I/CordovaWebView(2598): Disabled addJavascriptInterface() bridge since Android version is old.
04-30 15:57:06.285: D/CordovaActivity(2598): CordovaActivity.init()
04-30 15:57:06.315: D/CordovaWebView(2598): >>> loadUrl(file:///android_asset/www/index.html)
04-30 15:57:06.315: D/PluginManager(2598): init()
04-30 15:57:06.365: D/CordovaWebView(2598): >>> loadUrlNow()
04-30 15:57:06.365: I/CordovaLog(2598): Changing log level to DEBUG(3)
04-30 15:57:06.365: I/CordovaLog(2598): Found start page location: index.html
04-30 15:57:06.365: D/Whitelist(2598): Unlimited access to network resources
04-30 15:57:06.365: D/CordovaActivity(2598): Resuming the App
04-30 15:57:06.365: D/CordovaActivity(2598): CB-3064: The errorUrl is null
04-30 15:57:06.385: D/ViewRootImpl(2598): pckname = com.jadesync.travelguide
04-30 15:57:06.395: I/dalvikvm(2598): threadid=3: reacting to signal 3
04-30 15:57:06.425: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:06.425: I/dalvikvm(2598): Wrote stack traces to '/data/anr/traces.txt'
04-30 15:57:06.485: D/libEGL(2598): loaded /system/lib/egl/libEGL_mali.so
04-30 15:57:06.555: D/libEGL(2598): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-30 15:57:06.565: D/libEGL(2598): loaded /system/lib/egl/libGLESv2_mali.so
04-30 15:57:06.615: D/OpenGLRenderer(2598): Enabling debug mode 0
04-30 15:57:06.645: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:06.715: D/CordovaActivity(2598): onMessage(onPageStarted,file:///android_asset/www/index.html)
04-30 15:57:07.055: D/dalvikvm(2598): GC_CONCURRENT freed 124K, 3% free 6869K/7047K, paused 2ms+6ms
04-30 15:57:08.675: D/CordovaLog(2598): file:///android_asset/www/cordova.js: Line 966 : Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only.
04-30 15:57:08.675: I/Web Console(2598): Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only. at file:///android_asset/www/cordova.js:966
04-30 15:57:09.585: D/dalvikvm(2598): GC_CONCURRENT freed 24K, 3% free 7253K/7431K, paused 2ms+9ms
04-30 15:57:09.615: D/CordovaWebViewClient(2598): onPageFinished(file:///android_asset/www/index.html)
04-30 15:57:09.615: D/CordovaActivity(2598): onMessage(onPageFinished,file:///android_asset/www/index.html)
04-30 15:57:09.725: D/dalvikvm(2598): GC_FOR_ALLOC freed 11K, 3% free 7641K/7815K, paused 34ms
04-30 15:57:09.795: D/dalvikvm(2598): GC_FOR_ALLOC freed 9K, 3% free 8025K/8199K, paused 33ms
04-30 15:57:10.005: D/CordovaActivity(2598): onMessage(spinner,stop)
04-30 15:57:10.055: D/CordovaNetworkManager(2598): Connection Type: wifi
04-30 15:57:10.065: D/CordovaNetworkManager(2598): Connection Type: wifi
04-30 15:57:10.065: D/CordovaActivity(2598): onMessage(networkconnection,wifi)
04-30 15:57:11.625: D/CordovaActivity(2598): onMessage(spinner,stop)
04-30 15:57:19.155: D/CordovaWebViewClient(2598): onPageFinished(file:///android_asset/www/index.html#&ui-state=dialog)
04-30 15:57:19.155: D/CordovaActivity(2598): onMessage(onPageFinished,file:///android_asset/www/index.html#&ui-state=dialog)
04-30 15:57:24.555: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:25.455: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:28.465: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:31.005: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:31.105: W/webcoreglue(2598): updateFrameCache: pending style recalc, ignoring.
04-30 15:57:33.275: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:33.395: D/SoftKeyboardDetect(2598): Ignore this event
04-30 15:57:36.855: D/CordovaLog(2598): file:///android_asset/www/js/custom_script.js: Line 120 : success
04-30 15:57:36.855: I/Web Console(2598): success at file:///android_asset/www/js/custom_script.js:120
04-30 15:57:36.925: D/CordovaWebViewClient(2598): onPageFinished(file:///android_asset/www/index.html#/android_asset/www/index.html)
04-30 15:57:36.925: D/CordovaActivity(2598): onMessage(onPageFinished,file:///android_asset/www/index.html#/android_asset/www/index.html)
04-30 15:57:40.335: D/CordovaWebViewClient(2598): onPageFinished(file:///android_asset/www/index.html#view-my-packages)
04-30 15:57:40.335: D/CordovaActivity(2598): onMessage(onPageFinished,file:///android_asset/www/index.html#view-my-packages)
04-30 15:57:40.495: D/CordovaLog(2598): file:///android_asset/www/js/custom_script.js: Line 415 : [object Object]
04-30 15:57:40.495: I/Web Console(2598): [object Object] at file:///android_asset/www/js/custom_script.js:415
04-30 15:58:00.155: D/CordovaWebViewClient(2598): onPageFinished(file:///android_asset/www/index.html#view-my-packages&ui-state=dialog)
04-30 15:58:00.155: D/CordovaActivity(2598): onMessage(onPageFinished,file:///android_asset/www/index.html#view-my-packages&ui-state=dialog)
04-30 15:58:05.415: D/CordovaLog(2598): file:///android_asset/www/js/custom_script.js: Line 232 : Capture image
04-30 15:58:05.415: I/Web Console(2598): Capture image at file:///android_asset/www/js/custom_script.js:232
04-30 15:58:06.555: I/*********PictureSize************(2598): Supported Size. Width: 1280height : 1024
04-30 15:58:06.555: I/*********PictureSize************(2598): Supported Size. Width: 640height : 480
04-30 15:58:06.615: W/PluginManager(2598): THREAD WARNING: exec() call to Camera.takePicture blocked the main thread for 1192ms. Plugin should use CordovaInterface.getThreadPool().
04-30 15:58:06.625: D/CordovaActivity(2598): Paused the application!
04-30 15:58:06.625: D/CordovaWebView(2598): Handle the pause
04-30 15:58:06.705: D/OpenGLRenderer(2598): Flushing caches (mode 1)
04-30 15:58:06.735: D/OpenGLRenderer(2598): Flushing caches (mode 0)
04-30 15:58:07.555: D/CordovaActivity(2598): CordovaActivity.onDestroy()
04-30 15:58:07.625: D/CordovaWebView(2598): >>> loadUrlNow()
04-30 15:58:07.655: D/CordovaActivity(2598): onMessage(onPageStarted,about:blank)
04-30 15:58:07.695: D/CordovaWebViewClient(2598): onPageFinished(about:blank)
04-30 15:58:07.695: D/CordovaActivity(2598): onMessage(onPageFinished,about:blank)
04-30 15:58:07.695: D/CordovaActivity(2598): onMessage(exit,null)
Was it helpful?

Solution

SOLVED!!!

A very stupid mistake from my side... I never released the camera.

here's the correct code:

// **********TWEAK************* //

    camera = Camera.open(0);
    Camera.Parameters params = camera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPictureSizes();
    Camera.Size result = null;

    for (int i=0;i<sizes.size();i++){
        result = sizes.get(i);
        Log.i("*********PictureSize************", "Supported Size. Width: " + result.width + "height : " + result.height); 
    }

    camera.release();


// ********TWEAK ENDS********* //

note the last function call -- camera.release();

Hope it helps someone :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top