DocumentsContract.getDocumentId(uri)返回String路径而不是Long类型数字

近期在部分安卓手机中获取文件,发现部分安卓手机的uri路径中返回的不是 content://media/extenral/images/media/17766 这种常规类型的 地址
而是返回的是 content://media/extenral/images/media/raw:/storage/emulated/0/Download/my_file.pdf 类似这种的地址
这样在咱们常用的获取地址中就会报错了 java.lang.NumberFormatException' Exception

解决方法


image.png

修改isDownloadsDocument(uri) 中的方法按下述方法进行修改就完美解决了

else if (isDownloadsDocument(uri)) {
                final String id = DocumentsContract.getDocumentId(uri);
                if (!TextUtils.isEmpty(id)) {
                    if (id.startsWith("raw:")) {
                        return id.replaceFirst("raw:", "");
                    }
                    try {
                        final Uri contentUri = ContentUris.withAppendedId(
                                Uri.parse("content://downloads/public_downloads"), ContentUris.parseId(uri));
                        return getDataColumn(context, contentUri, null, null);
                    } catch (NumberFormatException e) {
                        Log.i("zhj", e.getMessage());
                        return null;
                    }
                }
            }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容