【问题标题】:How do I upload multiple images in my WebView?如何在我的 WebView 中上传多个图像?
【发布时间】:2014-10-04 19:06:12
【问题描述】:

如何使我应用的 WebView 中的文件上传行为与浏览器应用中的一样?

我正在尝试在我的 Android 应用程序中创建一个 WebView,以允许上传用相机拍摄的多张图像。

当我在浏览器应用程序中打开下面的 HTML 代码时,我可以附加多张图片。 当我的应用程序的 WebView 中有相同的代码时,按钮甚至不会打开对话框。

<form method="post" action="/save/images" name="send" id="send" enctype="multipart/form-data">
   <input type="file" name="data[]" id="camera" multiple="">
   <input type="submit" name="send">
</form>

如果您想尝试一下,这里有一个指向上述 HTML 的链接:http://codereaper.com/bugspray/so/25251993/html-sendimages/

这里的重点是 Android 应用程序本身与此无关,加载的 URL 包含一个网页,可在 Android 的浏览器应用程序中运行,您可以在其中使用相机上传一堆图像.

我目前的尝试状态包括授予应用权限:

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>

在创建 WebView 时允许 JavaScript 和 FileAcess:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report);

    WebView webView = (WebView) findViewById(R.id.report_webview);

    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setAllowFileAccess(true);

    showProgressDialog(R.string.please_wait_title, R.string.please_wait_description);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            closeProgressDialog();
        }
    }
    );

    webView.loadUrl("http://codereaper.com/bugspray/so/25251993/html-sendimages/");
}

在我的搜索中,我发现了很多使用未记录功能“openFileChooser”的参考:

   new WebChromeClient() {
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            Cv5appActivity.this.startActivityForResult(
                    Intent.createChooser(i, "Image Browser"),
                    FILECHOOSER_RESULTCODE);
        }
    }

我只支持较新的 Android 版本(4.3 及更高版本),并希望以支持和记录的方式执行此操作,但如果不存在其他方法,将使用此方法。但是我看不到“openFileChooser”方法将如何:

  • 允许拍摄/上传多张图片
  • 将捕获的图像交还给网页上的form/ajax/html

为了使 WebView 像 Android 上的浏览​​器应用程序一样或使 WebChromeClient 能够处理多个图像并“将它们交回 WebView”,任何建议都将不胜感激。

干杯

【问题讨论】:

  • 我没有尝试过,但它可能会有所帮助:stackoverflow.com/questions/18667881/…
  • @LarryMcKenzie 这似乎是 openFileChooser 方法的一个完整示例,但我仍然看不到它如何处理多个图像或将图像交还给 WebView。

标签: android file-upload webview


【解决方案1】:

不久前,我确实使用涉及“openFileChooser”方法的其他答案解决了我的所有问题。我需要发现/理解的关键信息是ValueCallback&lt;Uri&gt;,它既是处理所选图像的方式,也是将处理多个图像的责任交给 WebView。

在实现“openFileChooser”方法时,您会收到一个ValueCallback&lt;Uri&gt;,然后您负责调用其回调以返回所选图像或 null,例如:

mUploadMessage.onReceiveValue(null);

还要注意,如果你不调用回调,你的 WebView 将停止正常工作。

更新:

最近这个问题再次出现,所以现在我正在使用 https://github.com/delight-im/Android-AdvancedWebView 的一个分支 - 到目前为止它工作得很好并且我不必编写它。

【讨论】:

    【解决方案2】:

    将此行添加到onShowFileChooser:

    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    

    这段代码是用来解析结果的(注意uploadMessagesValueCallback&lt;Uri[]&gt;):

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
    
            Uri[] results = null;
            try {
                if (resultCode == RESULT_OK) {
                    String dataString = intent.getDataString();
                    ClipData clipData = intent.getClipData();
                    if (clipData != null) {
                        results = new Uri[clipData.getItemCount()];
                        for (int i = 0; i < clipData.getItemCount(); i++) {
                            ClipData.Item item = clipData.getItemAt(i);
                            results[i] = item.getUri();
                        }
                    }
                    if (dataString != null) {
                        results = new Uri[]{Uri.parse(dataString)};
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            uploadMessages.onReceiveValue(results);
            uploadMessages = null;
    
    
        }
    

    另见this post...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2018-11-09
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多