当我快速拍摄2张照片时,应用程序崩溃了。如何防止坠机?
注意:我可以拍几张照片,但拍照之间的增量时间必须在500毫秒左右。
这个
这是logcat:
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;
}
谢谢!
答案 0 :(得分:0)
可能不值得,但我会指出,从理论上讲,不可能以这种方式使用硬件。 当您尝试拍摄第二张照片时,硬件仍然用于拍摄第一张照片,因此,该程序不知道该去哪里,所以它会崩溃。
只是想我会指出这个因素,尽管我可能会因此而被投票。