【问题标题】:`getContentResolver().openInputStream(uri)` throws FileNotFoundException`getContentResolver().openInputStream(uri)` 抛出 FileNotFoundException
【发布时间】:2012-11-25 08:47:38
【问题描述】:

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

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

onActivityResult:

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

但它会在某些 android 设备上抛出 FileNotFoundException

uri的值:

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

抛出的异常:

java.io.FileNotFoundException: No such file or directory

而且很奇怪,它不会在其他一些安卓设备上抛出这个异常。什么会导致此错误以及如何解决?

【问题讨论】:

    标签: android image android-contentresolver


    【解决方案1】:
    MediaStore.Images.Media.INTERNAL_CONTENT_URI 
    

    对于本地设备上的图像或

    MediaStore.Images.Media.EXTERNAL_CONTENT_URI 
    

    用于 SD 卡上的图像。

    您确定这两个地址都正确吗?内部/外部处理因设备而异,也许这就是为什么它在某些设备上起作用而在其他设备上不起作用的原因。

    【讨论】:

    • 我刚刚发现是因为实际的图像被删除了,但是它们在照片查看器应用程序中有缓存。
    • 很高兴知道,在搜索问题时我遇到了一些解释相关问题 code.google.com/p/android/issues/detail?id=12508
    • Right Freewind,我在浏览我的下载文件夹以查找 .txt 文件时遇到了这个确切的代码/错误,并且在哪里可以下载,两者大小差不多,一个有效,一个没有。谢谢!
    【解决方案2】:

    我遇到了同样的问题,但我通过使用 ImageView 的 setImageURI 方法解决了它。

    您不必使用以下代码:

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

    只需使用以下函数:

    imageViewCustomer.setImageURI(data.getData());
    

    【讨论】:

      【解决方案3】:

      使用 Context.getContentResolver().openInputStream(Uri);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-19
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多