Lincy
Lincy

Reputation: 1093

Camera crashes in android 4.1(API level 16)

My application has a camera functionality. It works fine in all Android version but now when i tested in S3 it crashes. The error points to this line:

Parameters parameters = mCamera.getParameters();

In:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Log.e(TAG, "surfaceChanged");
    // XXX stopPreview() will crash if preview is not running
    if (mPreviewRunning) {
        mCamera.stopPreview();
    }

    /**
     * CODE TO RESOLVE CAMERA ORIENTATION PROBLEM START
     */

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
            .getDefaultDisplay();
    int flashStatus = session1.getFlashStatus();
    Parameters parameters = mCamera.getParameters();
    if (flashStatus == 0) {
        btnFlash.setBackgroundResource(R.drawable.flash);
        parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
        mCamera.setParameters(parameters);
    } else if (flashStatus == 1) {
        btnFlash.setBackgroundResource(R.drawable.flash_on);
        parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        mCamera.setParameters(parameters);
    } else {
        btnFlash.setBackgroundResource(R.drawable.flash_off);
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameters);
    }
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;

    if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO) {
        // Do something for froyo and above versions
        if (display.getRotation() == Surface.ROTATION_0) {
            // parameters.setPreviewSize(h, w);
            mCamera.setDisplayOrientation(90);
        }
        if (display.getRotation() == Surface.ROTATION_90) {
            // parameters.setPreviewSize(w, h);
        }
        if (display.getRotation() == Surface.ROTATION_180) {
            // parameters.setPreviewSize(h, w);
            mCamera.setDisplayOrientation(-90);
        }
        if (display.getRotation() == Surface.ROTATION_270) {
            // parameters.setPreviewSize(w, h);
            mCamera.setDisplayOrientation(90);
        }
    } else {
        // do something for phones running an SDK before level 9
        if (display.getOrientation() == Surface.ROTATION_0) {
            // parameters.setPreviewSize(h, w);
            mCamera.setDisplayOrientation(90);
        }
        if (display.getOrientation() == Surface.ROTATION_90) {
            mCamera.setDisplayOrientation(90);
            // parameters.setPreviewSize(w, h);
        }
        if (display.getOrientation() == Surface.ROTATION_180) {
            mCamera.setDisplayOrientation(90);
            // parameters.setPreviewSize(h, w);
        }
        if (display.getOrientation() == Surface.ROTATION_270) {
            mCamera.setDisplayOrientation(90);
            // parameters.setPreviewSize(w, h);
            // mCamera.setDisplayOrientation(180);
        }
    }

    mCamera.setParameters(parameters);
    /**
     * CODE TO RESOLVE CAMERA ORIENTATION PROBLEM END
     */
    try {
        mCamera.setPreviewDisplay(mSurfaceHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (mPreviewRunning) {
        mCamera.stopPreview();
        mCamera.startPreview();
        mPreviewRunning = true;
    } else {
        mCamera.startPreview();
        mPreviewRunning = true;
    }

}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e(TAG, "surfaceDestroyed");
    if (mPreviewRunning) {
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }
}

Could someone provide a solution for this? The log is below:

?:??: W/?(?): java.lang.NullPointerException
?:??: W/?(?):   at com.stpl.snapshun.camera.CameraActivity.surfaceChanged(CameraActivity.java:313)
?:??: W/?(?):   at android.view.SurfaceView.updateWindow(SurfaceView.java:554)
?:??: W/?(?):   at android.view.SurfaceView.access$000(SurfaceView.java:81)
?:??: W/?(?):   at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
?:??: W/?(?):   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
?:??: W/?(?):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
?:??: W/?(?):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
?:??: W/?(?):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
?:??: W/?(?):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
?:??: W/?(?):   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
?:??: W/?(?):   at android.view.Choreographer.doFrame(Choreographer.java:525)
?:??: W/?(?):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
?:??: W/?(?):   at android.os.Handler.handleCallback(Handler.java:615)
?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:92)
?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?):   at android.app.ActivityThread.main(ActivityThread.java:4745)
?:??: W/?(?):   at java.lang.reflect.Method.invokeNative(Native Method)
?:??: W/?(?):   at java.lang.reflect.Method.invoke(Method.java:511)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
?:??: W/?(?):   at dalvik.system.NativeStart.main(Native Method)

My issue here is trying to access camera parameters returns null.

Thanks in advance

Upvotes: 2

Views: 2364

Answers (4)

Lincy
Lincy

Reputation: 1093

German, see if this helps you get the idea:-

mCamera.autoFocus(myAutoFocusCallback);

/**
     * If camera having autofocus which is calling here
     */
    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean start, Camera arg1) {
            if (focus) {
                if (start) {
                    if (mPreviewRunning) {
                        mCamera.takePicture(myShutterCallback, mPicture,
                                mPicture);
                    }
                } else {
                    btnCamera.setEnabled(true);
                    side.setEnabled(true);
                    showToast("Could not get focus. Try again.");
                }
            } else {
                if (mPreviewRunning) {
                    mCamera.takePicture(myShutterCallback, mPicture, mPicture);
                }
            }
        }
    };


ShutterCallback myShutterCallback = new ShutterCallback() {

        @Override
        public void onShutter() {
            showToast("Picture taken!");
        }
    };

Upvotes: 0

Lincy
Lincy

Reputation: 1093

Sorry for the late update. The issue was when the device didn't have autofocus. So you need to first check whether the device has autofocus : boolean focus = getPackageManager().hasSystemFeature( "android.hardware.camera.autofocus");

Upvotes: 0

Usman Kurd
Usman Kurd

Reputation: 7023

Lincy Here is Code of Class Camera View

 public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) 
{   
    if(mPreviewRunning )
    {
        mCamera.stopPreview();
    }

    // Store width and height
    mWidth = width;
    mHeight = height;

    // Set camera parameters
    Camera.Parameters p = mCamera.getParameters();
    mCamera.setParameters(p);

    if(android.os.Build.VERSION.SDK_INT >= 8)
    {   // If API >= 8 -> rotate display...
        mCamera.setDisplayOrientation(90);
    }

    try
    {
        mCamera.setPreviewDisplay(holder);
    } catch(IOException e)
    {
        e.printStackTrace();
    }

    mCamera.startPreview();
    mPreviewRunning = true;

}
 public void surfaceCreated(final SurfaceHolder holder) 
{
    try {
        mCamera = Camera.open();
        mCamera.setPreviewDisplay(holder);
    } catch (IOException e) 
    {
        mCamera.release();
        mCamera = null;
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) 
{
    if(mCamera != null)
    {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
        mPreviewRunning = false;
        mCamera.release();
        mCamera = null;
    }   
}

one more Thing have you Implemented your class Using

SurfaceHolder.Callback

Upvotes: 2

VIGNESH
VIGNESH

Reputation: 2033

I too had the same problem with camera activity in S3 , and got fixed . it worked good for me .

I am not sure . if i am taking it right , you are using your own code for camera using surfaceview .

so for the higher android versions( < honeycomb) better launch the default camera .

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

and specifically for S3(captured image size goes till 4 Mb) , if u are uploading to web better resize it and upload . Because you get memory warnings and a crash again , for more than 2-3 picture captured and uploaded continuously .

Upvotes: 0

Related Questions