android webView webchromeclient 本地圖片資源

來源:互聯網
上載者:User

標籤:webview   調用圖片   

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

好了,大家去試試吧。至於不同的版本,大家可以在不同的函數裡面去實現哦。
謝謝大家!

android webView webchromeclient 本地圖片資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.