当我一张接一张地快速拍摄 2 张照片时,应用程序崩溃了。我怎样才能防止崩溃?
注意:我可以拍几张照片,但拍照之间的时间间隔必须在 500 毫秒左右。
这个
这是日志猫:
07-07 23:51:31.676: E/AndroidRuntime(10799): FATAL EXCEPTION: main
07-07 23:51:31.676: E/AndroidRuntime(10799): java.lang.IllegalStateException: Could not execute method of the activity
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.view.View$1.onClick(View.java:3063)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.view.View.performClick(View.java:3534)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.view.View$PerformClick.run(View.java:14263)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.os.Handler.handleCallback(Handler.java:605)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.os.Looper.loop(Looper.java:137)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.app.ActivityThread.main(ActivityThread.java:4441)
07-07 23:51:31.676: E/AndroidRuntime(10799): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 23:51:31.676: E/AndroidRuntime(10799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 23:51:31.676: E/AndroidRuntime(10799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 23:51:31.676: E/AndroidRuntime(10799): at dalvik.system.NativeStart.main(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799): Caused by: java.lang.reflect.InvocationTargetException
07-07 23:51:31.676: E/AndroidRuntime(10799): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.view.View$1.onClick(View.java:3058)
07-07 23:51:31.676: E/AndroidRuntime(10799): ... 11 more
07-07 23:51:31.676: E/AndroidRuntime(10799): Caused by: java.lang.RuntimeException: takePicture failed
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.hardware.Camera.native_takePicture(Native Method)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.hardware.Camera.takePicture(Camera.java:948)
07-07 23:51:31.676: E/AndroidRuntime(10799): at android.hardware.Camera.takePicture(Camera.java:893)
07-07 23:51:31.676: E/AndroidRuntime(10799): at com.inturnex.safecam.MainActivity.TakePicture(MainActivity.java:134)
07-07 23:51:31.676: E/AndroidRuntime(10799): ... 14 more
这是代码:
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_pick);
m_surfaceView = (SurfaceView)findViewById(R.id.preview);
m_surfaceView.getHolder().addCallback(this);
m_camera = Camera.open();
m_numOfCamera = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < m_numOfCamera; ++i) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
m_defaultCameraId = i;
m_currentCamera = m_defaultCameraId;
}
}
photoHandler = new PhotoHandler(this, dbHelper);
}
}
@Override
public void onPause() {
super.onPause();
if(m_camera != null){
m_camera.stopPreview();
}
}
@Override
public void onResume() {
super.onResume();
if(m_camera != null){
m_camera.startPreview();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(m_camera != null){
m_camera.release();
}
}
public void TakePicture(View v) throws InterruptedException
{
Log.d(Global.TAG,"In TakePicture");
m_camera.takePicture(null, null, photoHandler);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int format, int w, int h) {
m_surfaceWidth = w;
m_surfaceHeight = h;
Camera.Parameters params = m_camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = getOptimalPreviewSize(sizes, w, h);
params.setPreviewSize(selected.width, selected.height);
m_camera.setParameters(params);
setCameraDisplayOrientation(this, m_currentCamera, m_camera);
m_camera.startPreview();
}
static int degrees = 0;
private static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
public void surfaceCreated(SurfaceHolder arg0) {
try {
m_camera.setPreviewDisplay(m_surfaceView.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
谢谢!