我正在尝试从图库中选择图像,并且代码在高达andorid nogut(7)的设备上运行良好。我还添加了provider.xml。这是我的代码
public void processSelectionOfImage(int code) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"), code);
} catch (Exception e) {
e.printStackTrace();
}
}
我收到NullPointerException
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:453)
答案 0 :(得分:0)
android中没有内置方法getoutputmediafileuri()。这是用户编写的自定义方法,用于获取文件URI以将捕获的图像存储在特定目录中。您必须为其定义逻辑。而不是使用此代码,
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(),"MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent,0);