Skip to content

Commit 5d77c84

Browse files
authored
Merge pull request #276 from jakevossen5/fix_chrome_downloaded_files
fix(chrome-downloads): update to support pie api changes
2 parents 9068a80 + 6f3924e commit 5d77c84

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

src/imagepicker.android.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ class UriHelper {
2525
}
2626
// DownloadsProvider
2727
else if (UriHelper.isDownloadsDocument(uri)) {
28-
id = DocumentsContract.getDocumentId(uri);
29-
// Since Oreo the downloads id may be a raw string,
30-
// containing the file path:
31-
if (id.indexOf("raw:") !== -1) {
32-
return id.substring(4, id.length);
33-
}
34-
contentUri = android.content.ContentUris.withAppendedId(
35-
android.net.Uri.parse("content://downloads/public_downloads"), long(id));
36-
37-
return UriHelper.getDataColumn(contentUri, null, null);
28+
return UriHelper.getDataColumn(uri, null, null, true);
3829
}
3930
// MediaProvider
4031
else if (UriHelper.isMediaDocument(uri)) {
@@ -54,13 +45,13 @@ class UriHelper {
5445
let selection = "_id=?";
5546
let selectionArgs = [id];
5647

57-
return UriHelper.getDataColumn(contentUri, selection, selectionArgs);
48+
return UriHelper.getDataColumn(contentUri, selection, selectionArgs, false);
5849
}
5950
}
6051
else {
6152
// MediaStore (and general)
6253
if ("content" === uri.getScheme()) {
63-
return UriHelper.getDataColumn(uri, null, null);
54+
return UriHelper.getDataColumn(uri, null, null, false);
6455
}
6556
// FILE
6657
else if ("file" === uri.getScheme()) {
@@ -71,31 +62,57 @@ class UriHelper {
7162
return undefined;
7263
}
7364

74-
private static getDataColumn(uri: android.net.Uri, selection, selectionArgs) {
65+
private static getDataColumn(uri: android.net.Uri, selection, selectionArgs, isDownload: boolean) {
7566
let cursor = null;
76-
let columns = [android.provider.MediaStore.MediaColumns.DATA];
7767
let filePath;
78-
79-
try {
80-
cursor = this.getContentResolver().query(uri, columns, selection, selectionArgs, null);
81-
if (cursor != null && cursor.moveToFirst()) {
82-
let column_index = cursor.getColumnIndexOrThrow(columns[0]);
83-
filePath = cursor.getString(column_index);
84-
if (filePath) {
85-
return filePath;
68+
if (isDownload) {
69+
let columns = ["_display_name"];
70+
try {
71+
cursor = this.getContentResolver().query(uri, columns, selection, selectionArgs, null);
72+
if (cursor != null && cursor.moveToFirst()) {
73+
let column_index = cursor.getColumnIndexOrThrow(columns[0]);
74+
filePath = cursor.getString(column_index);
75+
if (filePath) {
76+
const dl = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS);
77+
filePath = `${dl}/${filePath}`;
78+
return filePath;
79+
}
80+
}
81+
}
82+
catch (e) {
83+
console.log(e);
84+
}
85+
finally {
86+
if (cursor) {
87+
cursor.close();
8688
}
8789
}
8890
}
89-
catch (e) {
90-
console.log(e);
91-
}
92-
finally {
93-
if (cursor) {
94-
cursor.close();
91+
else {
92+
let columns = [android.provider.MediaStore.MediaColumns.DATA];
93+
let filePath;
94+
95+
try {
96+
cursor = this.getContentResolver().query(uri, columns, selection, selectionArgs, null);
97+
if (cursor != null && cursor.moveToFirst()) {
98+
let column_index = cursor.getColumnIndexOrThrow(columns[0]);
99+
filePath = cursor.getString(column_index);
100+
if (filePath) {
101+
return filePath;
102+
}
103+
}
104+
}
105+
catch (e) {
106+
console.log(e);
107+
}
108+
finally {
109+
if (cursor) {
110+
cursor.close();
111+
}
95112
}
96113
}
97-
98114
return undefined;
115+
99116
}
100117

101118
private static isExternalStorageDocument(uri: android.net.Uri) {

0 commit comments

Comments
 (0)