Android WebView File Upload Android WebView File Upload android android

Android WebView File Upload


This is how i am using in my app

 private class MyWebChromeClient extends WebChromeClient {    //The undocumented magic method override    //Eclipse will swear at you if you try to put @Override here    // For Android 3.0+    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {        mUploadMessage = uploadMsg;        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);        intent.addCategory(Intent.CATEGORY_OPENABLE);        intent.setType("*/*");        startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);    }    //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("*/*");        startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);    }    protected void openFileChooser(ValueCallback<Uri> uploadMsg) {        mUploadMessage = uploadMsg;        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);        intent.addCategory(Intent.CATEGORY_OPENABLE);        intent.setType("*/*");        startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);    }    // For Lollipop 5.0+ Devices    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {        if (uploadMessage != null) {            uploadMessage.onReceiveValue(null);            uploadMessage = null;        }        uploadMessage = filePathCallback;        Intent intent = null;        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            intent = fileChooserParams.createIntent();        }        try {            startActivityForResult(intent, REQUEST_SELECT_FILE);        } catch (ActivityNotFoundException e) {            uploadMessage = null;            Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();            return false;        }        return true;    }


Mike Olivier's reference shared by Fr33dan is important. I am sharing what worked for me under situation quite akin yours.

wv.setWebChromeClient(new WebChromeClient()  {// For Android 3.0+public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {  mUploadMessage = uploadMsg;  Intent i = new Intent(Intent.ACTION_GET_CONTENT);  i.addCategory(Intent.CATEGORY_OPENABLE);  i.setType("image/*");  MainActivity.this.startActivityForResult( Intent.createChooser( i, getString(R.string.fileselect) ), MainActivity.FILECHOOSER_RESULTCODE ); }// For Android < 3.0public void openFileChooser( ValueCallback<Uri> uploadMsg ) {openFileChooser( uploadMsg, "" );}// For Android > 4.1public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){openFileChooser( uploadMsg, "" );}});

NOTE: I found this somewhere in stack overflow and m0s' blog.

Also, Ignore lint warnings. This one works pretty well for API 8 and above.


Given the high number of votes I'm guessing no one has noticed the 3rd comment (from David Esteves) contains a link with to answer to this question.

Michel Olivier said:

This solution also works for Honeycomb and Ice Cream Sandwich. Seems like Google introduced a cool new feature (accept attribute) and forgot to to implement an overload for backwards compatibility.

So you just need to add a openFileChooser overload to your MyWebChromeClient class that accepts the string parameter and then call the one that does not:

public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {      this.openFileChooser(uploadMsg);}

Doing this I was able to make your code run as intended.