Browse Source

Change media scanner interface

Ben Hsieh 8 years ago
parent
commit
d600648f55

+ 13
- 15
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

125
     }
125
     }
126
 
126
 
127
     @ReactMethod
127
     @ReactMethod
128
-    public void scanFile(ReadableMap pairs, Callback callback) {
129
-        ReadableMapKeySetIterator it = pairs.keySetIterator();
130
-        WritableArray path = Arguments.createArray();
131
-        WritableArray mimes = Arguments.createArray();
132
-        while(it.hasNextKey()) {
133
-            String key = pairs.keySetIterator().nextKey();
134
-            path.pushString(key);
135
-            String mime = pairs.getString(key);
136
-            mimes.pushString(mime);
137
-        }
138
-        String [] p = new String[path.size()];
139
-        String [] m = new String[path.size()];
140
-        for(int i =0;i<path.size();i++) {
141
-            p[i] = path.getString(i);
142
-            m[i] = mimes.getString(i);
128
+    public void scanFile(ReadableArray pairs, Callback callback) {
129
+        int size = pairs.size();
130
+        String [] p = new String[size];
131
+        String [] m = new String[size];
132
+        for(int i=0;i<size;i++) {
133
+            ReadableMap pair = pairs.getMap(i);
134
+            if(pair.hasKey("path")) {
135
+                p[i] = pair.getString("path");
136
+                if(pair.hasKey("mime"))
137
+                    m[i] = pair.getString("mime");
138
+                else
139
+                    m[i] = null;
140
+            }
143
         }
141
         }
144
         new RNFetchBlobFS(this.getReactApplicationContext()).scanFile(p, m, callback);
142
         new RNFetchBlobFS(this.getReactApplicationContext()).scanFile(p, m, callback);
145
     }
143
     }

+ 18
- 9
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

1
 package com.RNFetchBlob;
1
 package com.RNFetchBlob;
2
 
2
 
3
+import android.content.Intent;
3
 import android.media.MediaScannerConnection;
4
 import android.media.MediaScannerConnection;
4
 import android.net.Uri;
5
 import android.net.Uri;
5
 import android.os.AsyncTask;
6
 import android.os.AsyncTask;
414
     }
415
     }
415
 
416
 
416
     void scanFile(String [] path, String[] mimes, final Callback callback) {
417
     void scanFile(String [] path, String[] mimes, final Callback callback) {
417
-        try {
418
-            MediaScannerConnection.scanFile(mCtx, path, mimes, new MediaScannerConnection.OnScanCompletedListener() {
419
-                @Override
420
-                public void onScanCompleted(String s, Uri uri) {
421
-                    callback.invoke(null, true);
422
-                }
423
-            });
424
-        } catch(Exception err) {
425
-            callback.invoke(err.getLocalizedMessage(), null);
418
+//        try {
419
+//            MediaScannerConnection.scanFile(mCtx, path, mimes, new MediaScannerConnection.OnScanCompletedListener() {
420
+//                @Override
421
+//                public void onScanCompleted(String s, Uri uri) {
422
+//                    callback.invoke(null, true);
423
+//                }
424
+//            });
425
+//        } catch(Exception err) {
426
+//            callback.invoke(err.getLocalizedMessage(), null);
427
+//        }
428
+        for(String p : path) {
429
+            File file = new File(p);
430
+            Uri uri = Uri.fromFile(file);
431
+            Intent scanFileIntent = new Intent(
432
+                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
433
+            mCtx.sendBroadcast(scanFileIntent);
434
+            callback.invoke();
426
         }
435
         }
427
     }
436
     }
428
 
437
 

+ 3
- 6
src/fs.js View File

175
 
175
 
176
 /**
176
 /**
177
  * Android only method, request media scanner to scan the file.
177
  * Android only method, request media scanner to scan the file.
178
- * @param  {Array<string>} paths Paths of files to be scanned.
179
- * @param  {Array<string>} mimes Optional array of MIME types for each path.
180
- *                               If mimeType is null, then the mimeType will be
181
- *                               inferred from the file extension.
178
+ * @param  {Array<Object<string, string>>} Array contains Key value pairs with key `path` and `mime`.
182
  * @return {Promise}
179
  * @return {Promise}
183
  */
180
  */
184
-function scanFile(paths:Array<string>, mimes: Array<string>):Promise {
181
+function scanFile(pairs:any):Promise {
185
   return new Promise((resolve, reject) => {
182
   return new Promise((resolve, reject) => {
186
-    RNFetchBlob.scanFile(path, (err) => {
183
+    RNFetchBlob.scanFile(pairs, (err) => {
187
       if(err)
184
       if(err)
188
         reject(err)
185
         reject(err)
189
       else
186
       else