handle Camera.open() fail gracefully

This commit is contained in:
tibbi 2016-04-19 13:24:56 +02:00
parent 6ce7b5e2e4
commit d3d2f5e23d
3 changed files with 17 additions and 2 deletions

View File

@ -49,7 +49,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
public void setCamera(int cameraId) {
currCameraId = cameraId;
final Camera newCamera = Camera.open(cameraId);
Camera newCamera;
try {
newCamera = Camera.open(cameraId);
} catch (Exception e) {
Utils.showToast(getContext(), R.string.camera_open_error);
Log.e(TAG, "setCamera open " + e.getMessage());
return;
}
if (camera == newCamera) {
return;
}
@ -73,7 +81,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
Log.e(TAG, "setCamera " + e.getMessage());
Log.e(TAG, "setCamera setPreviewDisplay " + e.getMessage());
}
setupPreview();
}

View File

@ -1,6 +1,8 @@
package com.simplemobiletools.camera;
import android.content.Context;
import android.hardware.Camera;
import android.widget.Toast;
public class Utils {
public static Camera.CameraInfo getCameraInfo(int cameraId) {
@ -8,4 +10,8 @@ public class Utils {
Camera.getCameraInfo(cameraId, info);
return info;
}
public static void showToast(Context context, int resId) {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
}
}

View File

@ -1,3 +1,4 @@
<resources>
<string name="app_name">Simple Camera</string>
<string name="camera_open_error">An error occurred at obtaining the camera</string>
</resources>