如何在Android中允许相册对WebView的权限?

时间:2019-04-17 04:57:49

标签: java android webview

我正在使用URL加载WebView。我的网址包含一个按钮,可用于从照相馆或使用设备相机从手机拍摄照片。我还设置了访问照片库的权限。但是该按钮不会打开照片库。

这是我的代码:

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());

1 个答案:

答案 0 :(得分:1)

您可以像这样实现它

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;
    }
});      

您的html应该是这样的

<!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>