从 android 网络视图访问移动图库

Access the mobile gallery from an android webview

在我的应用程序中,我使用了 webview。 我要上传的网站 (https://www.editorfotosgratis.com/),当从桌面浏览器访问时(例如 chrome),您可以从计算机中选择一张照片,使用 "Buscar" 按钮,上传和编辑它 当我从 Android phone 的 Chrome 浏览器访问同一页面时,它会请求 CAMERA and WRITE / READ_EXTERNAL_STORAGE 的权限并打开移动设备的图像选择。

问题是当我从 WebView 加载此页面时,按 "Buscar" 按钮完全没有任何反应。 应用程序已授予必要的权限,已确认,我已尝试在 webview 中允许对文件的所有类型的访问:

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

有人能告诉我我做错了什么吗?

提前致谢

引自:https://www.whosebug.com/a/36413800

您需要创建一个 WebChromeClient

在 WebViewActivity 的 onCreate() 中执行此操作:

webView.setWebChromeClient(new WebViewChromeClient());

在同一个 class 创建一个 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;
    }
}

希望对您有所帮助!!