本文介绍: WebChromeClient @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { //此对象用来将获取到的uri回传给h5 h5filePathCallback=filePathCallba
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[]> 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可以得到选中的图片进行处理,显示或上传后显示
原文地址:https://blog.csdn.net/weixin_41648633/article/details/122092039
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_34764.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。