代码之家  ›  专栏  ›  技术社区  ›  keybee

Android在用户密码失败时拍照

  •  0
  • keybee  · 技术社区  · 11 年前

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

    可能是什么问题? 提前谢谢!

    1 回复  |  直到 11 年前
        1
  •  0
  •   keybee    11 年前

    takePicture 失败,因为必须有一个使用过的表面视图高于android 4.0(至少1x1像素)