Reputation: 91
I need to write an Android application which is capable of loading a Website via a WebView. The Website contains an Input (Type=FILE)
<form action="...">
<input type="file" name="myFile" value="" id="..."><
<input type="submit" value="submit"><
</form>
When loading is done, the application should use a specific Image and upload it via a storage path:
String Path = "/storage/emulated/0/myimage.jpg"
I already tried to open a FileChooser-Dialog and that works, but I need a solution without the filechooser. The path of the "Path"-Variable should be used. I know that this would be a huge security leak, but is there maybe a solution for this? Maybe change die input-value via JavaScript? Maybe with a Library?
PS: Not meant to do anything illegal - the company app is generating profile images and they need to be uploaded via an existing unchangeable Input of type File.
Thanks in advance.
Upvotes: 0
Views: 1335
Reputation: 2515
You can try this:
1) Get the URI of your file.
2) Trigger file chooser
3) Override onShowFileChooser
inside WebChromeClient
like this:
ValueCallback<Uri[]> mFilePathCallback;
@Override
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
mFilePathCallback.onReceiveValue(myURI);
mFilePathCallback = null;
return true;
}
Here myURI
is the URI
of your file.
Upvotes: 2
Reputation: 8835
Check my answer here. What you basically need to do is override WebChromeClient
and its method openFileChooser
.
class MyWebChromeClient extends WebChromeClient {
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e) {
uploadMessage = null;
Toast.makeText(WebLink.this, "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
}
protected void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
}
Upvotes: 0