我的webview嵌入了我的應用(Android 4.4.4+)。在頁面上有一個「上傳圖像」按鈕,我使用openFileChooser
進行管理。如何從webview上傳圖片?
在某些設備上,當我點擊webview中的「上傳圖片」按鈕時,相機應用程序啓動並且我的應用程序被銷燬。拍攝照片後,我的應用程序被重新創建,我恢復webview狀態(保存在onSaveInstanceState
),並調用onActivityResult()
。問題是包含ValueCallBack
的變量mUploadMessage
爲空,所以我無法調用ValueCallBack.onReceiveValue()
將圖像URI提供給webview。
// openFileChooser for Android 3.0+ to 4.4
// WARNING: the openFileChooser works in version 4.4 only for 4.4.3+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
// Update message
mUploadMessage = uploadMsg;
fileChooserManagement(FILECHOOSER_RESULTCODE);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webview.saveState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore last state
webview.restoreState(savedInstanceState);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Do not test the resultCode, because it's mandatory to enter the 'if' and to call the
// onReceiveValue even with null. Otherwise the openFileChooser will not be called anymore.
if (requestCode == FILECHOOSER_RESULTCODE) {
if (this.mUploadMessage == null) {
Log.i("myApp", "onActivityResult mUploadMessage is null");
return;
}
mUploadMessage.onReceiveValue(processPicture(resultCode, data));
mUploadMessage = null;
}
}
有類似的話題here,但這個問題:「我不知道如何保存網頁視圖狀態,並恢復它。如果我在onRestoreInstanceState恢復它()的頁面不會從相機拍照..你能給我一個例子嗎?「沒有答案。
謝謝你的幫助!