|
33 | 33 | import java.util.List;
|
34 | 34 | import java.util.Map;
|
35 | 35 |
|
| 36 | +import com.burnweb.rnsendintent.utils.RealPathUtil; |
36 | 37 | import com.facebook.react.bridge.ActivityEventListener;
|
37 | 38 | import com.facebook.react.bridge.BaseActivityEventListener;
|
38 | 39 | import com.facebook.react.bridge.Callback;
|
|
59 | 60 |
|
60 | 61 | public class RNSendIntentModule extends ReactContextBaseJavaModule {
|
61 | 62 |
|
62 |
| - private static final int FILE_SELECT_CODE = 20190903; |
| 63 | + private static final int FILE_SELECT_CODE = 0; |
63 | 64 | private static final String TAG = RNSendIntentModule.class.getSimpleName();
|
64 | 65 |
|
65 | 66 | private static final String TEXT_PLAIN = "text/plain";
|
@@ -778,13 +779,16 @@ public void openAllEmailApp() {
|
778 | 779 |
|
779 | 780 | @ReactMethod
|
780 | 781 | public void openFilePicker(ReadableMap options,Callback callback) {
|
| 782 | + //Needs permission "android.permission.READ_EXTERNAL_STORAGE" |
781 | 783 | mCallback = callback;
|
782 | 784 | 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") : "*/*"); |
784 | 788 | intent.addCategory(Intent.CATEGORY_OPENABLE);
|
785 | 789 | try {
|
786 | 790 | 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); |
788 | 792 | } catch (android.content.ActivityNotFoundException ex) {
|
789 | 793 |
|
790 | 794 | }
|
@@ -837,8 +841,24 @@ public void showIgnoreBatteryOptimizationsSettings() {
|
837 | 841 | @Override
|
838 | 842 | public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
|
839 | 843 | 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 | + } |
842 | 862 | }
|
843 | 863 | }
|
844 | 864 | };
|
|
0 commit comments