Skip to content Skip to sidebar Skip to footer

How Do I Upload Multiple Images In My Webview?

How can I make the file upload in my app's WebView behave like in the browser app? I am trying to make a WebView in my Android application allow uploading multiple images taken wit

Solution 1:

I did solve all my problems a while back using the other answers on SO that involves the 'openFileChooser' methods. The key piece of information I needed to discover/understand was the ValueCallback<Uri> which is both the way of handing the selected images back, but also puts the responsibility of handling multiple images to the WebView.

You are handed a ValueCallback<Uri> while implementing the 'openFileChooser' methods and you are then responsible for calling its callback to hand back with the selected image or null, like:

mUploadMessage.onReceiveValue(null);

Also note that if you do not call the callback, your WebView will stop working right.

UPDATE:

Recently the issue reappeared so now I am using a fork of https://github.com/delight-im/Android-AdvancedWebView - so far it is working beautifully and I didn't have to code it.

Solution 2:

add this line to onShowFileChooser:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

And this code is for parsing the result (Note that uploadMessages is a ValueCallback<Uri[]>):

publicvoidonActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        Uri[] results = null;
        try {
            if (resultCode == RESULT_OK) {
                StringdataString= intent.getDataString();
                ClipDataclipData= intent.getClipData();
                if (clipData != null) {
                    results = newUri[clipData.getItemCount()];
                    for (inti=0; i < clipData.getItemCount(); i++) {
                        ClipData.Itemitem= clipData.getItemAt(i);
                        results[i] = item.getUri();
                    }
                }
                if (dataString != null) {
                    results = newUri[]{Uri.parse(dataString)};
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        uploadMessages.onReceiveValue(results);
        uploadMessages = null;


    }

see also this post...

Post a Comment for "How Do I Upload Multiple Images In My Webview?"