android学习

DocumentsContract.getDocumentId(

2020-11-27  本文已影响0人  不会弹钢琴de大叔

近期在部分安卓手机中获取文件,发现部分安卓手机的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;
                    }
                }
            }
上一篇下一篇

猜你喜欢

热点阅读