当用户输入密码失败时,Android会拍照



我有一个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

有什么问题吗?提前感谢!

takePicture失败,因为android 4.0以上必须有一个使用过的表面视图(至少1x1 px)

相关内容

  • 没有找到相关文章

最新更新