瀏覽代碼

Correct app content provider URI handle #287

Ben Hsieh 7 年之前
父節點
當前提交
a8cfeb1d34

+ 23
- 6
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java 查看文件

@@ -138,11 +138,13 @@ public class RNFetchBlobFS {
138 138
      * @param promise
139 139
      */
140 140
     static public void readFile(String path, String encoding, final Promise promise ) {
141
-        path = normalizePath(path);
141
+        String resolved = normalizePath(path);
142
+        if(resolved != null)
143
+            path = resolved;
142 144
         try {
143 145
             byte[] bytes;
144 146
 
145
-            if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
147
+            if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
146 148
                 String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
147 149
                 long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
148 150
                 bytes = new byte[(int) length];
@@ -150,6 +152,14 @@ public class RNFetchBlobFS {
150 152
                 in.read(bytes, 0, (int) length);
151 153
                 in.close();
152 154
             }
155
+            // issue 287
156
+            else if(resolved == null) {
157
+                InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
158
+                int length = (int) in.available();
159
+                bytes = new byte[length];
160
+                in.read(bytes);
161
+                in.close();
162
+            }
153 163
             else {
154 164
                 File f = new File(path);
155 165
                 int length = (int) f.length();
@@ -226,7 +236,9 @@ public class RNFetchBlobFS {
226 236
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
227 237
      */
228 238
     public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
229
-        path = normalizePath(path);
239
+        String resolved = normalizePath(path);
240
+        if(resolved != null)
241
+            path = resolved;
230 242
         try {
231 243
 
232 244
             int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
@@ -234,9 +246,14 @@ public class RNFetchBlobFS {
234 246
                 chunkSize = bufferSize;
235 247
 
236 248
             InputStream fs;
237
-            if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
238
-                fs = RNFetchBlob.RCTContext.getAssets()
239
-                        .open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
249
+
250
+            if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
251
+                fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
252
+
253
+            }
254
+            // fix issue 287
255
+            else if(resolved == null) {
256
+                fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
240 257
             }
241 258
             else {
242 259
                 fs = new FileInputStream(new File(path));

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

@@ -64,8 +64,16 @@ public class PathResolver {
64 64
 
65 65
                 return getDataColumn(context, contentUri, selection, selectionArgs);
66 66
             }
67
+            else if ("content".equalsIgnoreCase(uri.getScheme())) {
68
+
69
+                // Return the remote address
70
+                if (isGooglePhotosUri(uri))
71
+                    return uri.getLastPathSegment();
72
+
73
+                return getDataColumn(context, uri, null, null);
74
+            }
67 75
             // Other Providers
68
-            else {
76
+            else{
69 77
                 try {
70 78
                     InputStream attachment = context.getContentResolver().openInputStream(uri);
71 79
                     if (attachment != null) {
@@ -131,6 +139,7 @@ public class PathResolver {
131 139
                                        String[] selectionArgs) {
132 140
 
133 141
         Cursor cursor = null;
142
+        String result = null;
134 143
         final String column = "_data";
135 144
         final String[] projection = {
136 145
                 column
@@ -141,13 +150,18 @@ public class PathResolver {
141 150
                     null);
142 151
             if (cursor != null && cursor.moveToFirst()) {
143 152
                 final int index = cursor.getColumnIndexOrThrow(column);
144
-                return cursor.getString(index);
153
+                result = cursor.getString(index);
145 154
             }
146
-        } finally {
155
+        }
156
+        catch (Exception ex) {
157
+            ex.printStackTrace();
158
+            return null;
159
+        }
160
+        finally {
147 161
             if (cursor != null)
148 162
                 cursor.close();
149 163
         }
150
-        return null;
164
+        return result;
151 165
     }
152 166
 
153 167