【发布时间】:2015-12-18 11:01:36
【问题描述】:
我的应用程序 (Android 4.4.4+) 中嵌入的 webview 出现问题。在页面上有一个“上传图片”按钮,我使用openFileChooser 管理。
在某些设备上,当我单击 web 视图中的“上传图像”按钮时,相机应用程序会启动并且我的应用程序会被销毁。拍照后我的应用程序被重新创建,我恢复了 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,但是问题“我不明白如何保存webview状态并恢复它。如果我在onRestoreInstanceState()中恢复它,页面不会从相机拍照..你能给我举个例子吗?”没有答案。
感谢你们的帮助!
【问题讨论】: