如何在 WebView 中上传多个图像?

Cod*_*per 4 android file-upload webview

如何使我的应用程序的 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>
Run Code Online (Sandbox Code Playgroud)

如果您想尝试一下,这里有一个指向上述 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"/>
Run Code Online (Sandbox Code Playgroud)

创建 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/");
}
Run Code Online (Sandbox Code Playgroud)

在我的搜索中,我发现了很多使用未记录功能“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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

干杯

fm-*_*sys 5

add this line to onShowFileChooser:

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Run Code Online (Sandbox Code Playgroud)

And this code is for parsing the result (Note that uploadMessages is a ValueCallback<Uri[]>):

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;


    }
Run Code Online (Sandbox Code Playgroud)

see also this post...