android webView webchromeclient 本地图片资源

解决android webView openFileChooser 不能调用本地文件

其实主要问题是出现在webChromeClient 的身上,通过查看webChromeClient的源代码我我们知道里面有个openFileChooser函数,不过很可惜,这个函数是不公开的,即使我们使用继承也不能使用这个函数。哈哈,那怎么办呢?
我们还是来看看这个函数具体长成啥样吧。
其实它是这样的
/**
* Tell the client to open a file chooser.
* @param uploadFile A ValueCallback to set the URI of the file to upload.
* onReceiveValue must be called to wake up the thread.a
* @param acceptType The value of the ‘accept‘ attribute of the input tag
* associated with this file picker.
* @param capture The value of the ‘capture‘ attribute of the input tag
* associated with this file picker.
* @hide
*/
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
uploadFile.onReceiveValue(null);
}
,是的它就是这样的。于是,那么问题来了,我们要怎么才能使用这个函数呢?哈哈,是不是已经猜到了?对哦,我们只要写个一样的函数,然后公开就可以了。于是,我们继承的类是这样的。

public class WebChromeClientEx extends WebChromeClient {

    // 用来实现webview js 调用本地图库的接口
    public void openFileChooser(ValueCallback<Uri> uploadFile) {

    }

    public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType) {

    }

    public void openFileChooser(ValueCallback<Uri> uploadFile,
            String acceptType, String capture) {

    }


}

好了,现在你会发现居然多出了两个函数,那么这是为什么呢?具体的源代码大家就自己去了解吧,我也不多费口舌了。
这样我们就可以这样调用了
@Override
public void openFileChooser(ValueCallback<Uri> uploadFile,
String acceptType, String capture) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
this.startActivityForResult(
Intent.createChooser(intent, "完成操作需要使用"),
FILECHOOSER_RESULTCODE);
}

好了,大家去试试吧。至于不同的版本,大家可以在不同的函数里面去实现哦。
谢谢大家!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。