我已经创建了几个相机应用程序,我刚刚在 3 周前将这些应用程序发布到 Google Play 市场。到目前为止,我收到了很多崩溃报告,如下所示:
android.hardware.Camera.native_setParameters 中的java.lang.RuntimeException
报告总数:2
com.potatotree.autodistance.CameraSurfaceView.surfaceCreated 中的java.lang.NullPointerException
报告总数:21
android.hardware.Camera.native_autoFocus 中的java.lang.RuntimeException
报告总数:2
android.hardware.Camera.native_setup 中的java.lang.RuntimeException
报告总数:12
到目前为止的总下载量约为 5000。似乎所有异常和崩溃都与相机有关。下面是CameraSurfaceView的源码和方法
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
private Context context;
public CameraSurfaceView(Context c) {
super(c);
context = c;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public CameraSurfaceView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CameraSurfaceView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}
public void surfaceCreated(SurfaceHolder holder) {
//once image is ready(captured or load), the surfaceCreated will not trigger camera again when user click on settings
mCamera = Camera.open();
try
{
mCamera.setPreviewDisplay(holder); //may throw exception
Parameters parameters = mCamera.getParameters();
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
}
else
{
parameters.set("orientation", "landscape");
mCamera.setDisplayOrientation(0);
}
mCamera.setParameters(parameters);
mCamera.startPreview();
}
catch(Exception e)
{
mCamera.release();
mCamera = null;
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder arg0) {
if(mCamera != null)
{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void startCamera() //resume camera
{
if(mCamera == null)
{
mCamera = Camera.open();
try
{
mCamera.setPreviewDisplay(mHolder);
Parameters parameters = mCamera.getParameters();
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("orientation", "portrait");
mCamera.setDisplayOrientation(90);
}
else
{
parameters.set("orientation", "landscape");
mCamera.setDisplayOrientation(0);
}
mCamera.setParameters(parameters);
mCamera.startPreview();
}
catch(Exception e)
{
mCamera.release();
mCamera = null;
e.printStackTrace();
}
}
}
public void stopCamera() //pause camera
{
if(mCamera != null)
{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void onFlashLight()
{
mCamera.stopPreview();
Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
public void offFlashLight()
{
mCamera.stopPreview();
Parameters parameters = mCamera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
public void takePicture()
{
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS))
{
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera)
{
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
}
else
{
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
public void autoFocus()
{
if(mCamera != null)
mCamera.autoFocus(null);
}
ShutterCallback shutterCallback = new ShutterCallback()
{
public void onShutter(){}
};
PictureCallback rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera){}
};
PictureCallback jpegCallback = new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera)
{
//something happens
}
};
}
这是我放在清单文件中的使用功能和使用权限
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
到目前为止,我已经在几台设备(Samsung Galaxy SII、Galaxy Note、Galaxy Nexus、Galaxy Y、HTC Desire HD、Sony Xperia)上测试了这个应用程序,当我在这些设备上运行应用程序时,我找不到任何问题或相机崩溃.
但问题是我不断收到崩溃报告和来自用户的电子邮件告诉我应用程序不断在他们的未知设备上崩溃。世界上有太多类型的 android 设备真的很沮丧,我无法在每台设备上测试我的应用程序以找出问题的根本原因,除非有人能找出我在源代码中犯的任何错误.
这是“ android.hardware.Camera.native_setParameters ”的堆栈跟踪
java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:1068)
at com.potatotree.autodistance.CameraSurfaceView.onFlashLight(CameraSurfaceView.java:168)
at com.potatotree.autodistance.AutoDistanceActivity.startCameraFlash(AutoDistanceActivity.java:224)
at com.potatotree.autodistance.CustomView.onTouchEvent(CustomView.java:876)
at android.view.View.dispatchTouchEvent(View.java:3885)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1779)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1130)
at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1763)
at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2196)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
这是“ com.potatotree.autodistance.CameraSurfaceView.surfaceCreated ”的堆栈跟踪
java.lang.NullPointerException
at com.potatotree.autodistance.CameraSurfaceView.surfaceCreated(CameraSurfaceView.java:97)
at android.view.SurfaceView.updateWindow(SurfaceView.java:1108)
at android.view.SurfaceView.access$000(SurfaceView.java:87)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1621)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2446)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
这是“ android.hardware.Camera.native_autoFocus ”的堆栈跟踪
java.lang.RuntimeException: autoFocus failed
at android.hardware.Camera.native_autoFocus(Native Method)
at android.hardware.Camera.autoFocus(Camera.java:832)
at com.potatotree.autodistance.CameraSurfaceView.autoFocus(CameraSurfaceView.java:212)
at com.potatotree.autodistance.AutoDistanceActivity.startCameraFocus(AutoDistanceActivity.java:235)
at com.potatotree.autodistance.CustomView.onTouchEvent(CustomView.java:880)
at android.view.View.dispatchTouchEvent(View.java:5604)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1787)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1787)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1787)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1917)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1376)
at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1865)
at android.view.View.dispatchPointerEvent(View.java:5784)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
这是“ android.hardware.Camera.native_setup ”的堆栈跟踪
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:294)
at android.hardware.Camera.open(Camera.java:269)
at com.potatotree.autodistance.CameraSurfaceView.surfaceCreated(CameraSurfaceView.java:72)
at android.view.SurfaceView.updateWindow(SurfaceView.java:552)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6994)
at android.widget.FrameLayout.draw(FrameLayout.java:357)
at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6994)
at android.widget.FrameLayout.draw(FrameLayout.java:357)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1896)
at android.view.ViewRoot.draw(ViewRoot.java:1527)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1263)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3735)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
at dalvik.system.NativeStart.main(Native Method)
有没有专家可以帮助我或者可以告诉我我的代码是否有任何错误?真的需要你的帮助!!谢谢