Sergio76
Sergio76

Reputation: 3986

Access the mobile gallery from an android webview

In my application I am using a webview. The web I want to upload (https://www.editorfotosgratis.com/), when accessed from a desktop browser (chrome for example) you can choose a photo from your computer, using the "Buscar" button, upload and edit it When I access the same page from the Chrome browser of my Android phone, it asks for the permissions of CAMERA and WRITE / READ_EXTERNAL_STORAGE and opens the selection of images of the mobile.

The problem is when I load this page from my WebView, pressing the "Buscar" button does absolutely nothing. The application has the necessary permissions granted, it is confirmed, and I have tried to allow in the webview all types of access to files:

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.loadUrl(url);

Could someone tell me what I'm doing wrong?

Thanks in advance

Upvotes: 1

Views: 3791

Answers (1)

Anonymous
Anonymous

Reputation: 1320

Quoted from : https://www.stackoverflow.com/a/36413800

You need to create a WebChromeClient.

in onCreate() of WebViewActivity do this :

webView.setWebChromeClient(new WebViewChromeClient());

In the same class create a MyWebChromeClient :

public class MyWebChromeClient extends WebChromeClient {
        // reference to activity instance. May be unnecessary if your web chrome client is member class.
    private MyActivity activity;

    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        // make sure there is no existing message
        if (myActivity.uploadMessage != null) {
            myActivity.uploadMessage.onReceiveValue(null);
            myActivity.uploadMessage = null;
        }

        myActivity.uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try {
            myActivity.startActivityForResult(intent, MyActivity.REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e) {
            myActivity.uploadMessage = null;
            Toast.makeText(myActivity, "Cannot open file chooser", Toast.LENGTH_LONG).show();
            return false;
        }

        return true;
    }
}

Hope it helps !!

Upvotes: 3

Related Questions