Skip to content

fix: openFilePicker bug #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,24 @@ SendIntentAndroid.openFileChooser(

## Example / Open File Picker

Opens Android own file selector to get the selected file and callback path from Uri
Please add these lines to your AndroidManifest.xml file before using next example:

```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```


Opens Android own file selector to get the selected file and callback path from Uri.
Allow multiple selection and don't forget check permission before using.

```javascript
SendIntentAndroid.openFilePicker(
{
type: "file_mimetype", //default is "*/*"
title: "selector title", //default is "Choose File"
type: "file_mimetype", //optional, default is "*/*"
title: "selector title", //optional, default is " "
multiple: true, //optional, default is false
},
filePath => {}
stringArr => {} //return filePaths string and need parse to array (minimum length = 1)
);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Map;

import com.burnweb.rnsendintent.utils.RealPathUtil;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Callback;
Expand All @@ -59,7 +60,7 @@

public class RNSendIntentModule extends ReactContextBaseJavaModule {

private static final int FILE_SELECT_CODE = 20190903;
private static final int FILE_SELECT_CODE = 0;
private static final String TAG = RNSendIntentModule.class.getSimpleName();

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

@ReactMethod
public void openFilePicker(ReadableMap options,Callback callback) {
//Needs permission "android.permission.READ_EXTERNAL_STORAGE"
mCallback = callback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(options.getString("type"));

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, options.hasKey("multiple") && options.getBoolean("multiple"));
intent.setType(options.hasKey("type") ? options.getString("type") : "*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
Activity currentActivity = getCurrentActivity();
currentActivity.startActivityForResult(Intent.createChooser(intent, options.getString("title")),FILE_SELECT_CODE);
currentActivity.startActivityForResult(Intent.createChooser(intent, options.hasKey("title") ? options.getString("title") : ""),FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {

}
Expand Down Expand Up @@ -837,8 +841,24 @@ public void showIgnoreBatteryOptimizationsSettings() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == FILE_SELECT_CODE && data!=null) {
Uri uri = data.getData();
mCallback.invoke(uri.getPath());
if(data.getData() != null){ // Single file picker

String res = RealPathUtil.getRealPath(reactContext, data.getData());
res = res != null ? "\""+res+"\"" : null;
mCallback.invoke("["+res+"]"); // min length = 1

} else if(data.getClipData() != null){ // Multiple files picker

int len = data.getClipData().getItemCount();
String[] result = new String[len];
for(int i = 0; i < len; i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();
String res = RealPathUtil.getRealPath(reactContext, uri);
result[i] = res != null ? "\""+res+"\"" : null;
}

mCallback.invoke(Arrays.toString(result));
}
}
}
};
Expand Down
Loading