Android"简单拍摄照片"教程对我不起作用

时间:2016-02-18 18:31:45

标签: java android android-camera android-camera-intent

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

我正在关注官方"简单拍摄照片"教程,但它只是给我错误。

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

以下是我得到的错误:

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.access$1400(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.access$1400(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(注意,它不会让我使用受保护的)

@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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

引用the documentation for ACTION_IMAGE_CAPTURE

  

调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象。这对于只需要小图像的应用程序非常有用。如果存在EXTRA_OUTPUT,则将将全尺寸图像写入EXTRA_OUTPUT的Uri值。

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