h5 打开android 相册

1. 先使用 android 打开webview

创建 webview 控件,对其进行设置

        webView = (WebView) findViewById(R.id.webview);

        webSettings = webView.getSettings();
        webSettings.setAppCacheEnabled(true);
        webSettings.setCacheMode(webSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setAllowFileAccess(true);

        //处理http和https混合的问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        } else {
            webSettings.setMixedContentMode(WebSettings.LOAD_NORMAL);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 允许javascript出错
            try {
                Method method = Class.forName("android.webkit.WebView").
                        getMethod("setWebContentsDebuggingEnabled", Boolean.TYPE);
                if (method != null) {
                    method.setAccessible(true);
                    method.invoke(null, true);
                }
            } catch (Exception e) {
                // do nothing
            }
        }

        webView.setFocusable(true);// 自选,非必要
        webView.setDrawingCacheEnabled(true); // 自选,非必要

        webView.setWebViewClient(new PQClient());
        webView.setWebChromeClient(new PQChromeClient());

2. webview设置 访问 相册方法

通过复写 WebChromeClient.onShowFileChooser

WebChromeClient.openFileChooser

来实现 打开系统图片资源

3.h5打开相册

辅助WebView处理图片上传操作【<input type=file> 文件上传标签】

4. 权限自动获取

activity 添加

@RuntimePermissions

主动获取权限

MainActivityPermissionsDispatcher.method_xWithPermissionCheck(this);

实现方法

@NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA, Manifest.permission.INTERNET})
void method_x() {
    //使用权限
    Toast.makeText(this, "已有权限", Toast.LENGTH_SHORT).show();
}

NeedsPermission 就是 本项目要获取的权限

使用到的第三方库:

permissionsdispatcher

避坑:

Android开发深入理解WebChromeClient之onShowFileChooser或openFileChooser使用说明_甜tianquan的博客-CSDN博客_android onshowfilechooser

发现 当点击取消的时候,下次再不再调用打开文件的操作。

原因就是上次的文件回调函数没有处理完成。

代码:

https://gitee.com/xjyylzh/h5openphotos.git

查找错误提示,使用浏览器打开

找到在项目目录下app/build/reports/lint-results-release-fatal.html 文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值