码迷,mamicode.com
首页 > 移动开发 > 详细

android webview 动态修改img src

时间:2020-09-17 22:16:19      阅读:39      评论:0      收藏:0      [点我收藏+]

标签:als   auth   select   onchange   setw   本地   das   get   cep   

  • h5:
  • <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="Generator" content="EditPlus?">
        <meta name="Author" content="">
        <meta name="Keywords" content="">
        <meta name="Description" content="">
        <title>H5页面上传手机照片</title>
    </head>
    <body>
    <script type="text/javascript">
        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);
            }
        }
    </script>
    
    <form action="">
        <!--    <p>图片1: <input type="file" name="uploadImg1"/></p>-->
        <!--    <p>图片2: <input type="file" name="uploadImg2"/></p>-->
        <!--    <input type="submit" value="Submit"/>-->
    
        <input accept="image/*" capture="camera" id="imgFile" name="imgFile" type="file"
               onchange="previewFile()"> <br/>
        <p></p>
        <img width="100px" height="100px" id="up_image"/>
    </form>
    
    </body>
    </html>
    
    • android
            private void webViewClient(WebView webView) {
                        webView.setWebChromeClient(new WebChromeClient() {
                // For Android 3.0+
                            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                    mUploadFile = uploadMsg;
                    openImageChooserOperator();
                }
    
                // For Android < 3.0
                public void openFileChooser(ValueCallback<Uri> uploadMsgs) {
                    mUploadFile = uploadMsgs;
                    openImageChooserOperator();
                }
    
                // For Android  > 4.1.1
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    mUploadFile = uploadMsg;
                    openImageChooserOperator();
                }
    
                @Override
                public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> 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<Uri> uploadMsg, String acceptType) {
                    mUploadFile = uploadMsg;
                    openImageChooserOperator();
                }
    
                // For Android < 3.0
                public void openFileChooser(ValueCallback<Uri> uploadMsgs) {
                    mUploadFile = uploadMsgs;
                    openImageChooserOperator();
                }
    
                // For Android  > 4.1.1
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    mUploadFile = uploadMsg;
                    openImageChooserOperator();
                }
    
                @Override
                public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> 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;
                    }
                }
            }
        }

    android webview 动态修改img src

    标签:als   auth   select   onchange   setw   本地   das   get   cep   

    原文地址:https://blog.51cto.com/xuguohongai/2531822

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!