瀏覽代碼

fix cannot import file from download folder in android

Danh Nguyen 4 年之前
父節點
當前提交
95207a99bc
共有 1 個檔案被更改,包括 18 行新增5 行删除
  1. 18
    5
      android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java

+ 18
- 5
android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java 查看文件

@@ -37,12 +37,25 @@ public class PathResolver {
37 37
             }
38 38
             // DownloadsProvider
39 39
             else if (isDownloadsDocument(uri)) {
40
+                try {
41
+                    final String id = DocumentsContract.getDocumentId(uri);
42
+                    //Starting with Android O, this "id" is not necessarily a long (row number),
43
+                    //but might also be a "raw:/some/file/path" URL
44
+                    if (id != null && id.startsWith("raw:/")) {
45
+                        Uri rawuri = Uri.parse(id);
46
+                        String path = rawuri.getPath();
47
+                        return path;
48
+                    }
49
+                    final Uri contentUri = ContentUris.withAppendedId(
50
+                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
40 51
 
41
-                final String id = DocumentsContract.getDocumentId(uri);
42
-                final Uri contentUri = ContentUris.withAppendedId(
43
-                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
44
-
45
-                return getDataColumn(context, contentUri, null, null);
52
+                    return getDataColumn(context, contentUri, null, null);
53
+                }
54
+                catch (Exception ex) {
55
+                    //something went wrong, but android should still be able to handle the original uri by returning null here (see readFile(...))
56
+                    return null;
57
+                }
58
+                
46 59
             }
47 60
             // MediaProvider
48 61
             else if (isMediaDocument(uri)) {