【问题标题】:How to upload image from webview?如何从 webview 上传图像?
【发布时间】: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()中恢复它,页面不会从相机拍照..你能给我举个例子吗?”没有答案。

感谢你们的帮助!

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    我知道你没有在你的代码的任何地方实现 webview 客户端。您需要同时实现 webview 并使用 web chrome 客户端类来处理您的 android 应用程序上的文件上传。此外,您应该覆盖 url 加载。此外,您需要添加访问 Internet 的权限和从外部存储读取的权限。您可以阅读更多内容并下载演示相同 here 的演示。希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2014-10-04
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2017-12-13
      相关资源
      最近更新 更多