Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

update downloads document cases #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/android/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,25 +343,42 @@ 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;
}
}
} finally {
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)) {
Expand Down