`getContentResolver()。openInputStream(URI)`抛出FileNotFoundException异常 [英] `getContentResolver().openInputStream(uri)` throws FileNotFoundException

查看:4696
本文介绍了`getContentResolver()。openInputStream(URI)`抛出FileNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个意图,让用户选择一张照片:

 意向意图=新意图(Intent.ACTION_PICK,
                           MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(意向,INTENT_SELECT_PHOTO);

而在的onActivityResult

 开放的URI = data.getData();
为InputStream的InputStream = getContentResolver()openInputStream(URI)。

但它抛出 FileNotFoundException异常上的一些Android设备。

的值 URI

 内容://媒体/外部/图像/媒体/ 26467

引发的异常:

  java.io.FileNotFoundException:没有这样的文件或目录

和它很奇怪,它不会抛出一些其他的Andr​​oid设备此异常。什么将导致此错误,以及如何解决它?


解决方案

  MediaStore.Images.Media.INTERNAL_CONTENT_URI

在本地设备上的图像或

  MediaStore.Images.Media.EXTERNAL_CONTENT_URI

在SD卡上的图像。

您一定要正确处理两者兼而有之?内部/外部治疗设备不同而不同,这也许就是为什么它的一些但不是别人的工作。

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?

解决方案

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.

这篇关于`getContentResolver()。openInputStream(URI)`抛出FileNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆