Android 相机错误 无法传递结果 ResultInfo

Android camera error Failure delivering result ResultInfo

我正在尝试让我的相机为我的 android 应用程序工作,但我不断收到以下错误

02-07 22:30:48.217 13197-13197/com.example.romsm.lap E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/8557 (has extras) }} to activity {com.example.romsm.lap/com.example.romsm.lap.TreeQuestionsActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3510) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3553) at android.app.ActivityThread.access00(ActivityThread.java:165) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5455) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) at dalvik.system.NativeStart.main(Native Method)

我已经尝试了所有可以在网上找到的方法,但似乎无法解决问题。 这是我做相机相关工作的代码。

btnPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                   // File file = new File(...);
                    startActivityForResult(takePictureIntent, ACTIVITY_START_CAMERA);
                   // takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                }
            }
        });



    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ACTIVITY_START_CAMERA) {
            if (data != null && resultCode == RESULT_OK) {
                // Image captured and saved to file
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imageView.setImageBitmap(imageBitmap);
            } else {
                // User cancelled the image capture or
                // image capture failed, advise user

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

这是我让相机工作的代码。你可以试试这个。

 private void activeTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {

                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
                //to generate random file name
                String fileName = "tempimg.jpg";

                try {
                    photo = (Bitmap) data.getExtras().get("data");
                    //captured image set in imageview
                    imageView.setImageBitmap(photo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }