3

I am loading WebView using URL. My URL contains one button which is used to take a photo from mobile either from photo gallery or using the device camera. I have also set the permissions to access the photo gallery. But the button not opens the photo gallery.

Here is my code:

webJobForm.getSettings().setLoadsImagesAutomatically(true);
webJobForm.getSettings().setPluginState(WebSettings.PluginState.ON);
webJobForm.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webJobForm.getSettings().setSupportZoom(true);
webJobForm.getSettings().setBuiltInZoomControls(true);
webJobForm.getSettings().setDisplayZoomControls(true);
webJobForm.getSettings().setLoadWithOverviewMode(true);
webJobForm.setVerticalScrollBarEnabled(true);
webJobForm.setHorizontalScrollBarEnabled(true);
webJobForm.getSettings().setUseWideViewPort(true);
webJobForm.getSettings().setAllowFileAccess(true);
webJobForm.getSettings().setAllowUniversalAccessFromFileURLs(true);
webJobForm.getSettings().setAllowContentAccess(true);
webJobForm.getSettings().setAllowFileAccessFromFileURLs(true);
webJobForm.getSettings().setMediaPlaybackRequiresUserGesture(true);
webJobForm.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webJobForm.getSettings().setJavaScriptEnabled(true);
webJobForm.setWebChromeClient(new WebChromeClient());
webJobForm.postUrl(bundle.getString("FormUrl"),JobID.getBytes());
James Z
  • 12,209
  • 10
  • 24
  • 44
PinkalB
  • 93
  • 6
  • Have you tried this https://stackoverflow.com/questions/28688946/using-a-webview-to-browse-the-photo-gallery – Remees M Syde Apr 17 '19 at 05:08
  • 1
    @RemeesMSyde. Thank you for quick reply :) There is no fixed html content in webview as in my case webview is loaded from url. – PinkalB Apr 17 '19 at 05:18
  • 2
    Try this [Question](https://stackoverflow.com/questions/29045637/html-input-type-file-is-not-working-on-webview-in-android-there-is-any-way-to). Tested and working – Ajith Madhu Apr 17 '19 at 05:21
  • 1
    @AjithMadhu Thank you so much, this solution worked for me. :) – PinkalB Apr 17 '19 at 05:48

1 Answers1

2

Well you can achieve it like this

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals("alert://alert")) {
            Toast.makeText(this, "alert", Toast.LENGTH_LONG).show();
        } else if (url.equals("choose://image")) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");

            startActivityForResult(intent, FILECHOOSER_RESULTCODE);
        }
        return true;
    }
});      

and your html should be like this

<!doctype html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Test Android Popup</title>
    </head>
    <body>
        <label>Test Alert 1:</label>
        <form action="alert://alert">
             <input type="submit" value="Click me!">
        </form>
        <br>
        <label>Test Browse file</label>
        <form action="image://choose">
            <input type="submit" value="Choose File">
        </form>
    </body>
</html>
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35