java.lang.IllegalArgumentException Unknown URI: content://downloads/public_downloads/ 解决方案

当使用如下代码调用安卓的自带文件选择

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
intent.addCategory(intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "请选择视频文件"),RequestCode);

如果在选择时使用的文件选择器为 下载内容 将会导致使用返回的URI获取绝对路径时出现类似如下错误:

java.lang.IllegalArgumentException

Unknown URI: content://downloads/public_downloads/1944

java.lang.RuntimeException:Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/1944 flg=0x1 }} to activity {com.equationl.videoshotpro/com.equationl.videoshotpro.MainActivity}: java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/1944

修复前使用的转换URI的部分代码如下:

public String getImageAbsolutePath(Activity context, Uri imageUri) {
      .............
      .............
       else if (isDownloadsDocument(imageUri)) {
                String id = DocumentsContract.getDocumentId(imageUri);
                if (id.startsWith("raw:")) {
                    final String path = id.replaceFirst("raw:", "");
                    return path;
                }
                 Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
                return getDataColumn(context, contentUri, null, null);
            } 
            .........
            .........
    }

通过查找资料,发现原来只需要将上述代码更改为:

public String getImageAbsolutePath(Activity context, Uri imageUri) {
      .............
      .............
       else if (isDownloadsDocument(imageUri)) {
                String id = DocumentsContract.getDocumentId(imageUri);
                if (id.startsWith("raw:")) {
                    final String path = id.replaceFirst("raw:", "");
                    return path;
                }
                Uri contentUri = imageUri;
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                    contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            } 
            .........
            .........
    }   

即可解决上述问题。
至于错误原因从上述代码中不难看出,在高于安卓O(8.0)版本时将URI设为

contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

会导致 Unknown URI 的问题,所以只需要判断一下当前的安卓版本,如果大于 O 则直接使用文件选择器返回的URI即可。

以上为我所使用的解决方案,对于不同的项目可能有不同的解决方案,可以看一下下面这项目里面的 issue
参考案例

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,568评论 0 17
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,600评论 1 92
  • 归纳 线性关系、线性表的定义,线性表的基本操作。 线性表的顺序存储结构与链式存储结构(包括单(向)链表、循环链表和...
    泽泽馥泽泽阅读 695评论 0 0
  • 今天画的中心图挺满意,每天的计划按部就班,有不停遗漏的点也有计划外体验到的点,都很好,日子过得很有印记
    茶语丝享阅读 460评论 6 1
  • 这张相片是去年暑假,我和妈妈在公园散步我给妈妈拍的照。今年妈妈78岁,身体硬朗,她最大的优点是许多事能够放下,能宽...
    glx凌香阅读 207评论 0 1