編輯:關於 Android 程式設計
用 WebView 載入自己的網頁,頁面上有個 <input type="file">,點下去卻什麼都不會發生。這是 WebView 上傳圖片最經典的坑,根因在 WebChromeClient。
翻 WebChromeClient 的源碼,會找到這麼一個方法:
/**
* 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.
* @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);
}
看清楚最後那行:onReceiveValue(null)——預設實作就是「回傳空值、什麼都不做」。而且這個方法帶著 @hide 標記,不屬於公開 API,所以你即使繼承 WebChromeClient 也覆寫不了它。
既然它存在於類別中,只是沒被公開,那我們就在自己的子類裡宣告一份同簽名的公開版本。Java 的方法解析會優先找到我們定義的那個。
public class WebChromeClientEx extends WebChromeClient {
/** 讓 WebView 的 JS 能呼叫本地圖庫 */
public void openFileChooser(ValueCallback<Uri> uploadFile) {
// 部分舊版本 WebView 只會呼叫這個兩參數以下的版本
}
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType) {
// 部分版本走這裡
}
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
// 較新版本走這裡
}
}
宣告之後你會發現「莫名多出兩個方法」——那是因為不同 Android 版本、不同 WebView 實作(尤其是各家 ROM 內核版本差異極大)呼叫的簽名不同,所以三個都補上最保險。
@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/*"); // 依 acceptType 動態決定更嚴謹
startActivityForResult(
Intent.createChooser(intent, "完成操作需要選擇檔案"),
FILECHOOSER_RESULTCODE);
}
只實作 openFileChooser 是不夠的。使用者選完圖片回到頁面,WebView 還在等那個 callback。必須在 Activity 中把結果補回去:
private ValueCallback<Uri> mUploadMessage;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILECHOOSER_RESULTCODE) {
if (mUploadMessage == null) {
return;
}
Uri result = (data == null || resultCode != RESULT_OK) ? null : data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null; // 必須清空,否則下次上傳會拿到舊 callback
}
}
並在 WebChromeClientEx 中把 uploadFile 存起來:
@Override
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
mUploadMessage = uploadFile;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "選擇圖片"), FILECHOOSER_RESULTCODE);
}
acceptType/capture 的版本,並期待你能同時處理「拍照」與「相簿」兩個來源,因此回傳值需要支援 ClipData(多選)與 WebChromeClient.FileChooserParams.parseResult() 這種官方工具方法。WebChromeClient.onShowFileChooser(WebView, ValueCallback, FileChooserParams),這是官方支援的入口,能覆寫、能拿到 acceptType。若你的 minSdkVersion 允許,優先用它,上面那套反射式寫法只留給舊版本相容。mUploadMessage 已為 null。可在 onSaveInstanceState 中記錄是否有未完成的上傳請求,回來後提示使用者重試。onReceiveValue(null) 並把引用設為 null,否則 WebView 的執行緒會一直等在那裡,同時造成記憶體洩漏。
Android RadioButton 與 RadioGroup 使用方法
RadioButton 與 CheckBox 差異RadioButton 通常用於單選情境。單顆 RadioButton 選取後無法再點擊取消;同一組 RadioGrou
Android View 體系:打造可左右滑動切換頁面的自定義 ViewGroup
為什麼要自定義 ViewGroup 在 Android View 體系中,自定義 ViewGroup 是理解測量、布局與事件分發的關鍵一步。本文以一個可左右滑動切換頁面的
Android 圓形旋轉菜單:互動效果展示
圓形旋轉菜單的特色圓形旋轉菜單是一種常見的創意互動模式,適合用在需要快速切換、分類瀏覽或視覺焦點導引的介面中。透過放射狀排列與旋轉動畫,使用者能以更直覺的方式探索功能。在
仿微信底部菜單欄:用TabHost實現未讀消息紅點提示
底部菜單欄幾乎是所有應用的標配。這裡用 TabHost 做了一個通用版本,重點是能像微信那樣在圖標上顯示未讀消息數量。之前也做過類似功能,但佈局嵌套層數太多顯得臃腫,這次