Browse Source

Apply possible fix to fs.readFile and fs.readStream for #287

Ben Hsieh 8 years ago
parent
commit
8121dbf66d
1 changed files with 20 additions and 4 deletions
  1. 20
    4
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 20
- 4
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -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();
@@ -225,7 +235,9 @@ public class RNFetchBlobFS {
225 235
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
226 236
      */
227 237
     public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
228
-        path = normalizePath(path);
238
+        String resolved = normalizePath(path);
239
+        if(resolved != null)
240
+            path = resolved;
229 241
         try {
230 242
 
231 243
             int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
@@ -233,9 +245,13 @@ public class RNFetchBlobFS {
233 245
                 chunkSize = bufferSize;
234 246
 
235 247
             InputStream fs;
236
-            if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
248
+            if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
237 249
                 fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
238 250
             }
251
+            // fix issue 287
252
+            else if(resolved == null) {
253
+                fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
254
+            }
239 255
             else {
240 256
                 fs = new FileInputStream(new File(path));
241 257
             }