Sid
Sid

Reputation: 489

StartPreview fails only on Galaxy S3

We keep getting crash reports that StartPreview failed with the following stack trace. This seems to work just fine when running on other devices. I tried to repro it using a S3 but couldn't. It seems to work just fine.

java.lang.RuntimeException: startPreview failed
at android.hardware.Camera.startPreview(Native Method)
at com.myapp.myservice.photo.PhotoCaptureView.surfaceChanged(PhotoCaptureView.java:293)
at android.view.SurfaceView.updateWindow(SurfaceView.java:621)
at android.view.SurfaceView.access$000(SurfaceView.java:93)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:182)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:864)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)

This seems to happen when there is a change in the preview. The runtime exception occurs when calling startPreview.

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)  
{  
    if (mCamera == null) {
        Log.w(TAG, "Surface changed but no current camera!");
        return;
    }

    if (mIsPreviewRunning) {
        mCamera.stopPreview();
        mIsPreviewRunning = false;
    }

    try {
        mCamera.setPreviewDisplay(holder);
    } catch (IOException ex) {
        Log.e(TAG, "surfaceChanged: Failed with IOException", ex);
    }

    mCamera.startPreview();
    mIsPreviewRunning = true;
}

Any help would be much appreciated. Thanks!

Upvotes: 2

Views: 1005

Answers (1)

yushulx
yushulx

Reputation: 12160

Probably it's caused by holder. In surfaceChanged, use SurfaceHolder sHolder = getHolder(); to check whether sHolder equals to holder. If not, use mCamera.setPreviewDisplay(sHolder); instead of mCamera.setPreviewDisplay(holder); If sHolder equals to holder. You can call mCamera.stopPreview(); before mCamera.startPreview();

Upvotes: 2

Related Questions