-
Notifications
You must be signed in to change notification settings - Fork 24.1k
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
android Webview can't open a dialog window (for select a upload-file) #5219
Comments
Hey lukaifu2002, thanks for reporting this issue! React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.
|
Hey. Android Wevbiew doesn't have a public API for this upto 4.3 (Though you could achieve it by using undocumented APIs). And it again breaks in 4.4.1 though 4.4.3. The API are different for most version of Android. I'm not sure it's worth it to add this to the core, since it's hacky, uses private APIs and not supported in all Android versions. |
@satya164 what about a fix that at least resolves this for latest versions of Android (whichever have stable APIs for this)? |
I have faced the same problem. Actually we can devise our own WebView for android to support this. The key point is to overwrite openFileChooser method in WebChromeClient. You can check react-native-webview-file-upload for detail. |
@jjshammas Feel free to give it a try, the code looks something like this, ...
mWebView.setWebChromeClient(new WebChromeClient() {
// For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY);
}
// For Android 3.0+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(acceptType);
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY);
}
// For Android 4.1+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(acceptType);
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY);
}
// For Android 5.0+
@SuppressLint("NewApi")
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mUploadMessageArr != null) {
mUploadMessageArr.onReceiveValue(null);
mUploadMessageArr = null;
}
mUploadMessageArr = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e) {
mUploadMessageArr = null;
Toast.makeText(getActivity(), getString(R.string.file_chooser_error), Toast.LENGTH_LONG).show();
return false;
}
return true;
}
});
...
@SuppressLint("NewApi")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_FILE_LEGACY) {
if (mUploadMessage == null) return;
Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
else if (requestCode == REQUEST_SELECT_FILE) {
if (mUploadMessageArr == null) return;
mUploadMessageArr.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
mUploadMessageArr = null;
}
} |
Hi @lukaifu2002 and other folks, I implemented an experimental feature in my custom Webview module: That can help you with file upload handle inside html forms ;) |
Hi there! This issue is being closed because it has been inactive for a while. But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/android-webview-cant-open-a-dialog-window-for-select-a-upload-file Product Pains has been very useful in highlighting the top bugs and feature requests: Also, if this issue is a bug, please consider sending a pull request with a fix. |
I have added a PR for this #12807 |
Over the weekend, this became a major pain point on a deadline I was working on. @dongyaQin's work is excellent (and was an incredible help after a lot of time spent trying to decipher this issue), but was hard to plug into an existing project. As such, I've reworked the project into a react-native library that can be installed via NPM into existing projects in the usual way 3rd party libraries are incorporated into react-native projects. For now, it still has the limitation of the original library - being only able to upload images - but the other information in this thread should provide a solid basis for expanding that to any files. Hoping to find some time next week to work on it, once I get through this current deadline! It can be installed using |
@Oblongmana Please do take a look at my PR #12807! I'm trying to get this into react-native WebView so there is no need for a 3rd party library. You can also try out my fork https://github.com/farazs/react-native/tree/webview_file_input |
@satya164 Thanks for the code example. I'm afraid, however, that you're missing Android 4.4 here (which might be an unsolvable issue, as the callback seems to be disabled completely in 4.4). |
when a 'upload file' webpage embed in Android Webview, the webpage can't open a file-dialog window (for select a file to upload ).
But the webpage can be work very well in a safari browser when i don't used android webview. When i choose intentAndroid to called the webpage , it can also work very well.
the line below doesn't work, even though it can be seen.
The text was updated successfully, but these errors were encountered: