Browse Source

Correct app content provider URI handle #287

Ben Hsieh 7 years ago
parent
commit
a8cfeb1d34

+ 23
- 6
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

138
      * @param promise
138
      * @param promise
139
      */
139
      */
140
     static public void readFile(String path, String encoding, final Promise promise ) {
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
         try {
144
         try {
143
             byte[] bytes;
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
                 String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
148
                 String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
147
                 long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
149
                 long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
148
                 bytes = new byte[(int) length];
150
                 bytes = new byte[(int) length];
150
                 in.read(bytes, 0, (int) length);
152
                 in.read(bytes, 0, (int) length);
151
                 in.close();
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
             else {
163
             else {
154
                 File f = new File(path);
164
                 File f = new File(path);
155
                 int length = (int) f.length();
165
                 int length = (int) f.length();
226
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
236
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
227
      */
237
      */
228
     public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
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
         try {
242
         try {
231
 
243
 
232
             int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
244
             int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
234
                 chunkSize = bufferSize;
246
                 chunkSize = bufferSize;
235
 
247
 
236
             InputStream fs;
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
             else {
258
             else {
242
                 fs = new FileInputStream(new File(path));
259
                 fs = new FileInputStream(new File(path));

+ 18
- 4
android/src/main/java/com/RNFetchBlob/Utils/PathResolver.java View File

64
 
64
 
65
                 return getDataColumn(context, contentUri, selection, selectionArgs);
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
             // Other Providers
75
             // Other Providers
68
-            else {
76
+            else{
69
                 try {
77
                 try {
70
                     InputStream attachment = context.getContentResolver().openInputStream(uri);
78
                     InputStream attachment = context.getContentResolver().openInputStream(uri);
71
                     if (attachment != null) {
79
                     if (attachment != null) {
131
                                        String[] selectionArgs) {
139
                                        String[] selectionArgs) {
132
 
140
 
133
         Cursor cursor = null;
141
         Cursor cursor = null;
142
+        String result = null;
134
         final String column = "_data";
143
         final String column = "_data";
135
         final String[] projection = {
144
         final String[] projection = {
136
                 column
145
                 column
141
                     null);
150
                     null);
142
             if (cursor != null && cursor.moveToFirst()) {
151
             if (cursor != null && cursor.moveToFirst()) {
143
                 final int index = cursor.getColumnIndexOrThrow(column);
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
             if (cursor != null)
161
             if (cursor != null)
148
                 cursor.close();
162
                 cursor.close();
149
         }
163
         }
150
-        return null;
164
+        return result;
151
     }
165
     }
152
 
166
 
153
 
167