Java/Android Studio -无法传递结果ResultInfo

7gcisfzg  于 2022-12-09  发布在  Android
关注(0)|答案(2)|浏览(115)

在我的应用程序中,用户可以选择从照片库中选择图片,也可以选择用相机拍摄照片。但是,当用户选择要使用的照片时(当他们点击它时),应用程序崩溃,日志中出现以下错误-
java.lang.RuntimeException:无法将结果传递到com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/46/ORIGINAL/NONE/859067274活动{com. veterinaryseedations.georgeberdovskiy. veterinaryseedations. dogilActivity}}:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.widget.ImageView.setImageURI(android.net.Uri)”
这是我的代码-

public void PickImage(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final String[] weightUnitOptions = {"Camera", "Gallery"};
    builder.setTitle("Pick Image from")
            .setItems(weightUnitOptions, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(takePicture, 0);
                    } else {
                        Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(pickPhoto , 1);
                    }
                }

            });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
}

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (resultCode != RESULT_CANCELED) {
        if (requestCode == 0) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        } else if (requestCode == 1) {
            Uri selectedImage = imageReturnedIntent.getData();
            imageView.setImageURI(selectedImage);
        } else {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        }
    }
}

下面是我在StackOverflow上尝试使用的答案之一-

有人知道我需要做什么来修复这个错误吗?提前感谢!

8wtpewkr

8wtpewkr1#

您可以尝试此方法从相机获取图像:-

File destination;

    public void cameraIntent() {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        Uri mImageCaptureUri;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            destination = new File(Environment
                .getExternalStorageDirectory(),"IMG_"+System.currentTimeMillis() + ".jpg");
            mImageCaptureUri = Uri.fromFile(destination);
        } else {
            destination = new File(getFilesDir(), "IMG_" + System.currentTimeMillis() + ".jpg");
            mImageCaptureUri = Uri.parse("content://<Your package name or Directory name where you want to store>/");
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
}

并在onActivityResult方法中添加以下行:-

if (requestCode == 0) {
            Uri selectedImageUri = Uri.fromFile(destination);
        }
sbdsn5lh

sbdsn5lh2#

事实证明,这个问题非常简单,实际上在评论中得到了解答--我没有足够重视这个错误,也没有注意到结尾提到的空对象引用确实是ImageView,我忘记初始化了。但是,我还是要感谢回答这个问题的用户--它可能会对其他在这个主题上有类似问题的人有所帮助。

相关问题