WebView中的文件选择
2021-09-09 本文已影响0人
zhujunhua
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")