Android "Taking Photos Simply" 教程对我不起作用

Android "Taking Photos Simply" tutorial does not work for me

所以,我正在尝试在我的应用程序中实现拍照功能。

我正在按照官方 "Taking Photos Simply" 教程进行操作,但它只是给我错误。

我唯一不同的是,我使用的是弹出对话框片段。

下面是我得到的错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=null} to activity {com.myapp.demo/com.myapp.demo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3605)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3648)
        at android.app.ActivityThread.access00(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.paulcanning.mote.NoteDialogFragment.onActivityResult(NoteDialogFragment.java:179)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
        at android.app.Activity.dispatchActivityResult(Activity.java:6192)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3601)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3648)
            at android.app.ActivityThread.access00(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

按钮点击监听器

takePhoto.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
                //startActivityForResult(takePictureIntent, 1);

                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, 1);
                }
            }
        }

    });

onActivityResult(注意,它不允许我使用 protected)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == getActivity().RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageView.setImageBitmap(imageBitmap);
    }
}

您正在供应 EXTRA_OUTPUT。因此,相机应用程序不需要提供 getExtra("data"),因此通常为 null

引用 the documentation for ACTION_IMAGE_CAPTURE:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

您的图片(希望如此)位于您在 EXTRA_OUTPUT 中提供的位置。