Open imxiaopeng opened 3 years ago
谢谢兄弟的代码分享。不过,在安卓11上有问题哦。
我从标准的文件选择器里,选择一个Download文件夹下的PDF文件,"content://downloads/public_downloads" 这个uri,查询cursor时会捕获到“无效uri”的异常。我改成MediaStore.Downloads.EXTERNAL_CONTENT_URI. 不报无效uri了,但是查询到的cursor不是null却MovetoFirst()==false. 查询不到记录!
对于MediaProvider的PDF文件, 我使用的MediaStore.Files.getContentUri("external"),也是查询到的cursor不是null而movetoFirst()==false, 查询不到记录。
是否在安卓11上,标准的文件选择器里,无论是从 “下载”、“文档” 还是 “最近”选取的PDF文件,都不再允许查询到uri table 记录了?
谢谢!
public static String getPath(final Context context, final Uri uri) { MatchLog.E(RestRequest.TAG, "uri=" + uri); if (DocumentsContract.isDocumentUri(context, uri)) { if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); MatchLog.E(RestRequest.TAG, "docId=" + docId); if (!TextUtils.isEmpty(docId) && docId.startsWith("raw:")) { return Environment.getExternalStorageDirectory() + "/" + docId.substring(4); } final String[] split = docId.split(":"); /final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; }/ return Environment.getExternalStorageDirectory() + "/" + split[1]; } else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); if (!TextUtils.isEmpty(id) && id.startsWith("raw:")) { return id.substring(4); } final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } else { contentUri = MediaStore.Files.getContentUri("external"); } final String selection = "_id=?"; final String[] selectionArgs = new String[]{split[1]}; return getDataColumn(context, contentUri, selection, selectionArgs); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { if (isGooglePhotosUri(uri)) { String lastPathSegment = uri.getLastPathSegment(); MatchLog.E(RestRequest.TAG, "isGooglePhotosUri=" + lastPathSegment); return lastPathSegment; } if (isQQMediaDocument(uri)) { String path = uri.getPath(); File fileDir = Environment.getExternalStorageDirectory(); File file = new File(fileDir, path.substring("/QQBrowser".length(), path.length())); String pathStr = file.exists() ? file.toString() : null; MatchLog.E(RestRequest.TAG, "isQQMediaDocument=" + pathStr); return pathStr; } return getDataColumn(context, uri, null, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; }