From 91aea5fde7301db89f549aad5feabb58054e8afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luciano=20Rodr=C3=ADguez?= Date: Sun, 17 May 2020 19:35:36 -0300 Subject: [PATCH] update downloads document cases support more cases in provider com.android.providers.downloads.documents, applying some ideas taken from https://github.com/coltoscosmin/FileUtils/blob/master/FileUtils.java - fixed: added file check, I think the following case was never called. - possible uri added to the second case - try to download the content, this adds support to google drive documents (and possibly others) that are displayed under this provider if the file was recently used. - default return --- src/android/FilePath.java | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/android/FilePath.java b/src/android/FilePath.java index 8f835d4..17bf037 100644 --- a/src/android/FilePath.java +++ b/src/android/FilePath.java @@ -343,7 +343,7 @@ else if (isDownloadsDocument(uri)) { if (cursor != null && cursor.moveToFirst()) { String fileName = cursor.getString(0); String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName; - if (!TextUtils.isEmpty(path)) { + if (fileExists(path)) { return path; } } @@ -351,17 +351,34 @@ else if (isDownloadsDocument(uri)) { if (cursor != null) cursor.close(); } - // + final String id = DocumentsContract.getDocumentId(uri); - try { - final Uri contentUri = ContentUris.withAppendedId( - Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); - return getDataColumn(context, contentUri, null, null); - } catch(NumberFormatException e) { - //In Android 8 and Android P the id is not a number - return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", ""); + if (id != null && id.startsWith("raw:")) { + return id.substring(4); } + + String[] contentUriPrefixesToTry = new String[]{ + "content://downloads/public_downloads", + "content://downloads/my_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) { + return path; + } + } catch (Exception e) {} + } + + try { + return getDriveFilePath(uri, context); + } catch (Exception e) { + return uri.getPath(); + } + } // MediaProvider else if (isMediaDocument(uri)) {