我有一个DeviceAdminReceiver,当用户输入密码时我想用前置摄像头拍照 - 但是我收到了错误...
代码:
@Override
public void onPasswordFailed(Context context, Intent intent) {
try {
takePictureNoPreview(context);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void takePictureNoPreview(Context context) throws IOException {
// open back facing camera by default
Camera myCamera = Camera.open();
if (myCamera != null) {
try {
// set camera parameters if you want to
// ...
// here, the unused surface view and holder
SurfaceView dummy = new SurfaceView(context);
myCamera.setPreviewDisplay(dummy.getHolder());
myCamera.startPreview();
myCamera.takePicture(null, null, getJpegCallback()); //ERROR HERE, LINE 85
} finally {
// myCamera.close();
}
} else {
// booo, failed!
}
}
private PictureCallback getJpegCallback() {
PictureCallback jpeg = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos;
try {
fos = new FileOutputStream("test.jpeg");
fos.write(data);
fos.close();
} catch (IOException e) {
// do something about it
}
}
};
return jpeg;
}
错误日志:
03-20 08:01:35.427: E/AndroidRuntime(26930): FATAL EXCEPTION: main
03-20 08:01:35.427: E/AndroidRuntime(26930): java.lang.RuntimeException: Unable to start receiver packagename.receivername: java.lang.RuntimeException: takePicture failed
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.os.Looper.loop(Looper.java:137)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 08:01:35.427: E/AndroidRuntime(26930): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 08:01:35.427: E/AndroidRuntime(26930): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 08:01:35.427: E/AndroidRuntime(26930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 08:01:35.427: E/AndroidRuntime(26930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 08:01:35.427: E/AndroidRuntime(26930): at dalvik.system.NativeStart.main(Native Method)
03-20 08:01:35.427: E/AndroidRuntime(26930): Caused by: java.lang.RuntimeException: takePicture failed
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.hardware.Camera.native_takePicture(Native Method)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.hardware.Camera.takePicture(Camera.java:1095)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.hardware.Camera.takePicture(Camera.java:1040)
03-20 08:01:35.427: E/AndroidRuntime(26930): at packagename.receivername.takePictureNoPreview(DeviceAdmin.java:85)
03-20 08:01:35.427: E/AndroidRuntime(26930): at packagename.receivername.onPasswordFailed(DeviceAdmin.java:49)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.app.admin.DeviceAdminReceiver.onReceive(DeviceAdminReceiver.java:305)
03-20 08:01:35.427: E/AndroidRuntime(26930): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)
03-20 08:01:35.427: E/AndroidRuntime(26930): ... 10 more
可能是什么问题? 提前谢谢!
答案 0 :(得分:0)
takePicture
失败了,因为Android 4.0上面必须有一个使用过的表面视图(至少1x1像素)