webview h5选择图片/视频并显示

WebChromeClient

  @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback 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 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 && data != null && 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可以得到选中的图片进行处理,显示或上传后显示

你可能感兴趣的:(android,webview,音视频,android)