Android 在WebView中加载H5传递图片

最近h5开发一个编译器,要在手机上显示,需要获取手机上的图片,使用webview不能直接到文件管理拿取,还需要对webview做处理,做个记录,方便以后使用;

public ValueCallback mUploadMessageForAndroid5;

用这个方法去给定义的字段赋值,并使用方法跳转到手机本地拿到图片;一定要返回 true ,否则会报错的;

binding.webView.setWebChromeClient(new WebChromeClient(){

    // For Android => 5.0
    public boolean onShowFileChooser (WebView webView, ValueCallback uploadMsg,
                                      WebChromeClient.FileChooserParams fileChooserParams) {
        mUploadMessageForAndroid5 = uploadMsg;
        onenFileChooseImpleForAndroid(uploadMsg);
        return true;
    }

});

拿到用户文件返回的并返回给webview显示;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri result = data.getData();
    mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
    mUploadMessageForAndroid5 = null;
}

跳转手机本地图片的方法;

private void onenFileChooseImpleForAndroid(ValueCallback filePathCallback) {
    mUploadMessageForAndroid5 = filePathCallback;
    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType("image/*");

    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");

    startActivityForResult(chooserIntent, 2);
}

以上只做了跳转拿取图片,没有做文件或者视频的,如果以后有用到再更新。

你可能感兴趣的:(Android,android)