WebChromeClient

  @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            //此对象用来将获取到的uri回传给h5
                h5filePathCallback=filePathCallback;
                Intent intent;
                if (Build.VERSION.SDK_INT < 19) {
                    intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*,video/*");
                    ((BaseActivity) mContext).startActivityForResult(intent, BaseCanstant.REQ_CHOOSE_FILE);
                } else {
                    intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*,video/*");
                    Intent wrapperIntent = Intent.createChooser(intent, null);
                    ((BaseActivity) mContext).startActivityForResult(wrapperIntent, BaseCanstant.REQ_CHOOSE_FILE);
                }
                return true;
            }
  private ValueCallback<Uri[]&gt; h5filePathCallback;
    public void onReceiveValue(Uri[] uris){
        if(h5filePathCallback!=null){
            h5filePathCallback.onReceiveValue(uris);
        }
    }

activityResult回调获取图片/视频

 case BaseCanstant.REQ_CHOOSE_FILE:
                    try {
                        String imageStr = "";
                        if (resultCode == Activity.RESULT_OK) {
                            if (data != null) {
                                Integer count = 1;
                                ClipData images = null;
                                try {
                                    images = data.getClipData();
                                }catch (Exception e) {
                                    Log.e("Error!", e.getLocalizedMessage());
                                }

                                if (images == null &amp;&amp; data != null &amp;&amp; data.getDataString() != null) {
                                    count = data.getDataString().length();
                                } else if (images != null) {
                                    count = images.getItemCount();
                                }
                                Uri[] results = new Uri[count];
                                // Check that the response is a good one
                                if (resultCode == Activity.RESULT_OK) {
                                    if (data.getClipData() == null) {
                                        results = new Uri[]{Uri.parse(data.getDataString())};
                                    } else {
                                        for (int i = 0; i < images.getItemCount(); i++) {
                                            results[i] = images.getItemAt(i).getUri();
                                        }
                                    }
                                }
                               onReceiveValue(results);  //调用上边onReceiveValue方法设置onShowFileChooser,中filePathCallback
                            }
                        }
                    } catch (Exception e) {
                    }
                    break;

效果
H5页面打开图库,选择图片后,H5可以得到选中图片进行处理显示上传显示

原文地址:https://blog.csdn.net/weixin_41648633/article/details/122092039

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_34764.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注