我的应用程序具有相机功能。它在所有 Android 版本中都可以正常工作,但现在当我在 S3 中测试时它崩溃了。错误指向这一行:
Parameters parameters = mCamera.getParameters();
在:
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();
}
}
有人可以为此提供解决方案吗?日志如下:
?:??: 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)
我的问题是尝试访问相机参数返回 null。
提前致谢