Camera.takePicture(Camera $ ShutterCallback,Camera $ PictureCallback,Camera $ PictureCallback)'在null对象引用上

时间:2018-06-15 07:15:32

标签: android android-camera android-broadcastreceiver android-camera2

我看了很多问题并回答堆栈溢出但我无法找到问题解决方案。 我尝试直接从相机获取拍摄的图像并将后台任务提供给编辑图像,然后保存到原始位置。

我的代码是:

manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    String action = intent.getAction();
    final Handler handler = new Handler();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                manager.registerAvailabilityCallback(new CameraManager.AvailabilityCallback() {
                    @Override
                    public void onCameraAvailable(String cameraId) {
                        super.onCameraAvailable(cameraId);

                    }

                    @Override
                    public void onCameraUnavailable(String cameraId) {
                        super.onCameraUnavailable(cameraId);
                        //Do your work
                        camera.takePicture(myShutterCallback,
                                myPictureCallback_RAW, myPictureCallback_JPG);

                        myShutterCallback = new Camera.ShutterCallback(){

                            @Override
                            public void onShutter() {
                                // TODO Auto-generated method stub
                                Toast.makeText(context, "Shutter Clicked", Toast.LENGTH_SHORT).show();

                            }};

                        myPictureCallback_RAW = new Camera.PictureCallback(){

                            @Override
                            public void onPictureTaken(byte[] arg0, Camera arg1) {
                                // TODO Auto-generated method stub
                                Toast.makeText(context, "Raw Callback", Toast.LENGTH_SHORT).show();
                            }};

                        myPictureCallback_JPG = new Camera.PictureCallback(){

                            @Override
                            public void onPictureTaken(byte[] arg0, Camera arg1) {
                                // TODO Auto-generated method stub
                                Toast.makeText(context, "Image Captured", Toast.LENGTH_SHORT).show();
                                Bitmap bitmapPicture
                                        = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
                            }};
                        Toast.makeText(context, "Available Camera", Toast.LENGTH_SHORT).show();
                    }

                },handler);
            }

1 个答案:

答案 0 :(得分:0)

onStartCommand 方法中更改返回值 START_STICKY

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
       receiver = new CameraEventReceiverr();
       iFilter = new IntentFilter(Intent.ACTION_POWER_CONNECTED);
       iFilter.addAction(Intent.ACTION_CAMERA_BUTTON);
       iFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
       registerReceiver(receiver, iFilter);
       return START_STICKY;
    }