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);
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值