android动态替换src,android webview 动态修改img src

h5:

H5页面上传手机照片

function previewFile() {

// 仅限上传单张图片

var file = document.querySelector('#imgFile').files[0];

var reader = new FileReader();

reader.addEventListener("load", function () {

var imageNode = document.getElementById("up_image");

imageNode.src = reader.result; // 存储在本地的图片的base64编码

` imageNode.load();//必须加上,否则不会刷新`

alert("result=" + imageNode.src);

}, false);

if (file) {

reader.readAsDataURL(file);

}

}

οnchange="previewFile()">

android

private void webViewClient(WebView webView) {

webView.setWebChromeClient(new WebChromeClient() {

// For Android 3.0+

public void openFileChooser(ValueCallback uploadMsg, String acceptType) {

mUploadFile = uploadMsg;

openImageChooserOperator();

}

// For Android < 3.0

public void openFileChooser(ValueCallback uploadMsgs) {

mUploadFile = uploadMsgs;

openImageChooserOperator();

}

// For Android > 4.1.1

public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {

mUploadFile = uploadMsg;

openImageChooserOperator();

}

@Override

public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {

mUploadFiles = filePathCallback;

openImageChooserOperator();

//mUploadFiles.onReceiveValue(new Uri[]{Uri.parse("content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20200901_074953.jpg")});

//mUploadFiles.onReceiveValue(new Uri[]{Uri.parse("content://media/external/images/media/17714")});

return true;

}

@Override

public boolean onJsAlert(WebView view, String url, String message, JsResult result) {

Manager.getInstance().getUtils().showLog("message:" + message + "jsResult" + result.toString());

// Toast.makeText(mActivity,"message:" + message,Toast.LENGTH_SHORT).show();

return true;

}

@Override

public void onPermissionRequest(PermissionRequest request) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

request.grant(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE});

}

super.onPermissionRequest(request);

}

});

private void webViewClient(WebView webView) {

webView.setWebChromeClient(new WebChromeClient() {

// For Android 3.0+

public void openFileChooser(ValueCallback uploadMsg, String acceptType) {

mUploadFile = uploadMsg;

openImageChooserOperator();

}

// For Android < 3.0

public void openFileChooser(ValueCallback uploadMsgs) {

mUploadFile = uploadMsgs;

openImageChooserOperator();

}

// For Android > 4.1.1

public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {

mUploadFile = uploadMsg;

openImageChooserOperator();

}

@Override

public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {

mUploadFiles = filePathCallback;

openImageChooserOperator();

return true;

}

});

public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK) {

if (requestCode == fileUploadRequestCode) {//图片上传

Manager.getInstance().getUtils().showLog(data.getDataString());

if (mUploadFile != null) {

Uri result = data == null ? null : data.getData();

mUploadFile.onReceiveValue(result);

mUploadFile = null;

}

if (mUploadFiles != null) {

Uri result = data == null ? null : data.getData();

mUploadFiles.onReceiveValue(new Uri[]{result});

mUploadFiles = null;

}

}

} else if (resultCode == Activity.RESULT_CANCELED) {

if (requestCode == fileUploadRequestCode) {//图片上传

if (mUploadFile != null) {

mUploadFile.onReceiveValue(null);

mUploadFile = null;

}

if (mUploadFiles != null) {

mUploadFiles.onReceiveValue(null);

mUploadFiles = null;

}

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值