美文网首页
WebView中的文件选择

WebView中的文件选择

作者: zhujunhua | 来源:发表于2021-09-09 10:17 被阅读0次

    html示例

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,maximun-scale=1,mininum-scale=1,user-scale=1">
        <tile>文件选择</tile>
        <style>
            .filechooser {
                width: 95%;
                height: 50px;
                -webkit-background-clip: border-box;
            }
        </style>
        <script>
            var display = function(){
                var path = document.getElementById("file_chooser").textContent;
                document.getElementById("filechooser_display").innerHTML("<b>"+path+"</b>");
            }
        </script>
    </head>
    
    <body>
    <ul class="listview">
        <li class="listviewItem">
            <fieldset class="itemset">
                <input class="filechooser" id="file_chooser" type="file" accept="image/*" capture="camera" placeholder="选择文件" onchange="display()" oninput="display()"><br>
                <p id="filechooser_display"></p>
                <div class="line_black"></div>
            </fieldset>
        </li>
    </ul>
    </body>
    
    </html>
    
    

    Android代码

    webChromeClient = object : WebChromeClient() {
    
        @Keep
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        fun openFileChooser(
            valueCallback: ValueCallback<*>?,
            acceptType: String?) {
            Log4j.d(TAG, "#openFileChooser(Honeycomb), acceptType $acceptType")
        }
    
        @Keep
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        fun openFileChooser(
            valueCallback: ValueCallback<Uri?>?,
            acceptType: String?, capture: String?) {
            Log4j.d(TAG, "#openFileChooser(JellyBean), acceptType $acceptType, capture $capture")
            mFilePathCallback = valueCallback
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        override fun onShowFileChooser(
            webView: WebView?,
            filePathCallback: ValueCallback<Array<Uri>>?,
            fileChooserParams: FileChooserParams?
        ): Boolean {
            mFilePathCallback4Android5 = filePathCallback
            return true
        }
    }
    
    // 本地html
    webView.loadUrl("file:///android_asset/fileChooser.html")
    

    参考:
    Android使用WebView加载网页选择文件上传

    相关文章

      网友评论

          本文标题:WebView中的文件选择

          本文链接:https://www.haomeiwen.com/subject/yvdiwltx.html