`getContentResolver().openInputStream(uri)` throws FileNotFoundException

跟風遠走 提交于 2019-11-28 07:23:01

问题


I use this intent to let user select a photo:

Intent intent = new Intent(Intent.ACTION_PICK, 
                           MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, INTENT_SELECT_PHOTO);

And in onActivityResult:

Uri uri = data.getData();
InputStream inputStream = getContentResolver().openInputStream(uri);

But it throws FileNotFoundException on some android devices.

The value of uri:

content://media/external/images/media/26467

The exception thrown:

java.io.FileNotFoundException: No such file or directory

And it's very strange that it won't throw this exception on some other android devices. What will cause this error and how to fix it?


回答1:


MediaStore.Images.Media.INTERNAL_CONTENT_URI 

for images on the local device or

MediaStore.Images.Media.EXTERNAL_CONTENT_URI 

for images on the SD card.

Are you sure you are addressing both correctly? The internal/external treatment varies with device, maybe that is why its working on some but not on others.




回答2:


I face the same problem but I resolved it by using the setImageURI method of the ImageView.

You dont have to use the following code:

InputStream inputStream = getContentResolver().openInputStream(uri);

Simply use the following function:

imageViewCustomer.setImageURI(data.getData());



回答3:


Use Context.getContentResolver().openInputStream(Uri);



来源:https://stackoverflow.com/questions/13549559/getcontentresolver-openinputstreamuri-throws-filenotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!