近期在部分安卓手機中獲取文件,發(fā)現(xiàn)部分安卓手機的uri路徑中返回的不是 content://media/extenral/images/media/17766 這種常規(guī)類型的 地址
而是返回的是 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;
}
}
}