安卓文件选择报错:java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/571

南楼画角 提交于 2019-12-03 02:34:24

参考文章 链接: link.

1、报错日志

测试手机为8.0时
java.lang.NumberFormatException: For input string: "raw:/storage/emulated/0/Download/browser/100167000013.xls"
    at java.lang.Long.parseLong(Long.java:590)
    at java.lang.Long.valueOf(Long.java:804)
 测试手机为9.0时
  java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/571
		at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
		at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
		at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
		at android.content.ContentResolver.query(ContentResolver.java:804)
		at android.content.ContentResolver.query(ContentResolver.java:753)
		at android.content.ContentResolver.query(ContentResolver.java:711)	

2、图片选择为下载目录文件时(最开始的获取方法为):

} else if (isDownloadsDocument(uri)) {
          return getDataColumn(context, uri, null, null);
  }
public static String getDataColumn(Context context, Uri uri,
			String selection, String[] selectionArgs) throws Exception{
		Cursor cursor = null;
		final String column = "_data";
		final String[] projection = { column };
try{
			cursor = context.getContentResolver().query(uri, projection,
					selection, selectionArgs, null);
			if(cursor != null && cursor.moveToFirst()){
				final int column_index = cursor.getColumnIndexOrThrow(column);
				return cursor.getString(column_index);
			}
		}catch (Exception e){
			//G3Logger.debug(TAG + "getDataColumn>>Exception");
		}finally{
			if(cursor != null)
				cursor.close();
		}
		return null;
	}

8.0的修复方案只需要添加判断即可:raw:/storage/emulated/0/Download/browser/100167000013.xls

final String docId = DocumentsContract.getDocumentId(uri);
				if (docId.startsWith("raw:")) {
					final String path = docId.replaceFirst("raw:", "");
					return path;
				}

9.0的修复方案较为模糊

第一步 新增两种URI (好像没有效果)

String[] contentUriPrefixesToTry = new String[]{
		"content://downloads/public_downloads",
		"content://downloads/my_downloads",
		"content://downloads/all_downloads"
};

for (String contentUriPrefix : contentUriPrefixesToTry) {
	Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
	try {
		String path = getDataColumn(context, contentUri, null, null);
		if (path != null) {
			G3Logger.debug("isDownloadsDocument(uri)>>>获取地址成功---" + path);
			return path;
		}
	} catch (Exception e) {

	}
}

第二步 (获取成功) 如果前两个获取失败则进行第三方式 把文件复制到工程的缓存目录中
文件路径为 /data/user/0/包名/cache/documents/100167000013.xls

// path could not be retrieved using ContentResolver, therefore copy file to accessible cache using streams
String fileName = FileUtils.getFileName(context, uri);
File cacheDir = FileUtils.getDocumentCacheDir(context);
File file = FileUtils.generateFileName(fileName, cacheDir);
String destinationPath = null;
if (file != null) {
	destinationPath = file.getAbsolutePath();
	FileUtils.saveFileFromUri(context, uri, destinationPath);
}

G3Logger.debug("isDownloadsDocument(uri)--FileUtils获取地址>>>" + destinationPath);
return destinationPath;

FileUtils.java 参考链接 link.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!