Skip to content

Commit f02dfb3

Browse files
committed
fix: openFilePicker bug
1 parent cb69a5d commit f02dfb3

File tree

5 files changed

+375
-11
lines changed

5 files changed

+375
-11
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,24 @@ SendIntentAndroid.openFileChooser(
406406

407407
## Example / Open File Picker
408408

409-
Opens Android own file selector to get the selected file and callback path from Uri
409+
Please add these lines to your AndroidManifest.xml file before using next example:
410+
411+
```xml
412+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
413+
```
414+
415+
416+
Opens Android own file selector to get the selected file and callback path from Uri.
417+
Allow multiple selection and don't forget check permission before using.
410418

411419
```javascript
412420
SendIntentAndroid.openFilePicker(
413421
{
414-
type: "file_mimetype", //default is "*/*"
415-
title: "selector title", //default is "Choose File"
422+
type: "file_mimetype", //optional, default is "*/*"
423+
title: "selector title", //optional, default is " "
424+
multiple: true, //optional, default is false
416425
},
417-
filePath => {}
426+
stringArr => {} //return filePaths string and need parse to array (minimum length = 1)
418427
);
419428
```
420429

android/src/main/java/com/burnweb/rnsendintent/RNSendIntentModule.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.List;
3434
import java.util.Map;
3535

36+
import com.burnweb.rnsendintent.utils.RealPathUtil;
3637
import com.facebook.react.bridge.ActivityEventListener;
3738
import com.facebook.react.bridge.BaseActivityEventListener;
3839
import com.facebook.react.bridge.Callback;
@@ -59,7 +60,7 @@
5960

6061
public class RNSendIntentModule extends ReactContextBaseJavaModule {
6162

62-
private static final int FILE_SELECT_CODE = 20190903;
63+
private static final int FILE_SELECT_CODE = 0;
6364
private static final String TAG = RNSendIntentModule.class.getSimpleName();
6465

6566
private static final String TEXT_PLAIN = "text/plain";
@@ -778,13 +779,16 @@ public void openAllEmailApp() {
778779

779780
@ReactMethod
780781
public void openFilePicker(ReadableMap options,Callback callback) {
782+
//Needs permission "android.permission.READ_EXTERNAL_STORAGE"
781783
mCallback = callback;
782784
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
783-
intent.setType(options.getString("type"));
785+
786+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, options.hasKey("multiple") && options.getBoolean("multiple"));
787+
intent.setType(options.hasKey("type") ? options.getString("type") : "*/*");
784788
intent.addCategory(Intent.CATEGORY_OPENABLE);
785789
try {
786790
Activity currentActivity = getCurrentActivity();
787-
currentActivity.startActivityForResult(Intent.createChooser(intent, options.getString("title")),FILE_SELECT_CODE);
791+
currentActivity.startActivityForResult(Intent.createChooser(intent, options.hasKey("title") ? options.getString("title") : ""),FILE_SELECT_CODE);
788792
} catch (android.content.ActivityNotFoundException ex) {
789793

790794
}
@@ -837,8 +841,24 @@ public void showIgnoreBatteryOptimizationsSettings() {
837841
@Override
838842
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
839843
if (requestCode == FILE_SELECT_CODE && data!=null) {
840-
Uri uri = data.getData();
841-
mCallback.invoke(uri.getPath());
844+
if(data.getData() != null){ // Single file picker
845+
846+
String res = RealPathUtil.getRealPath(reactContext, data.getData());
847+
res = res != null ? "\""+res+"\"" : null;
848+
mCallback.invoke("["+res+"]"); // min length = 1
849+
850+
} else if(data.getClipData() != null){ // Multiple files picker
851+
852+
int len = data.getClipData().getItemCount();
853+
String[] result = new String[len];
854+
for(int i = 0; i < len; i++) {
855+
Uri uri = data.getClipData().getItemAt(i).getUri();
856+
String res = RealPathUtil.getRealPath(reactContext, uri);
857+
result[i] = res != null ? "\""+res+"\"" : null;
858+
}
859+
860+
mCallback.invoke(Arrays.toString(result));
861+
}
842862
}
843863
}
844864
};

0 commit comments

Comments
 (0)