【问题标题】:File Chooser not works in Samsung Tab 3 Android 4.4文件选择器不适用于三星 Tab 3 Android 4.4
【发布时间】:2015-07-21 15:48:23
【问题描述】:

我有一个带有文件选择器的 webview,它在 Samsung Galaxy Tab 2(Android 4.1.1), Lenovo, Nexus Tablets 中工作。但问题是它没有在Samsung galaxy Tab 3 中工作。这是Android 4.4 tablet。我在这里添加了我的setWebChromeClient 代码。你能请人帮助我。

// implement WebChromeClient inner class
        // we will define openFileChooser for select file from camera
        webView.setWebChromeClient(new WebChromeClient() {

            // openFileChooser for Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                    String acceptType) {
                /** updated, out of the IF **/
                mUploadMessage = uploadMsg;
                /** updated, out of the IF **/
                Log.e("Reac", "**Here");
                try {
                    File imageStorageDir = new File(base_directory,
                            "profile_pictures");
                    if (!imageStorageDir.exists()) {
                        imageStorageDir.mkdirs();
                    }
                    src_file = new File(imageStorageDir + File.separator
                            + "IMG_" + child_id + ".jpg");
                    mCapturedImageURI = Uri.fromFile(src_file); // save to the
                                                                // private
                                                                // variable

                    final Intent captureIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            mCapturedImageURI);

                    startActivityForResult(captureIntent,
                            FILECHOOSER_RESULTCODE);
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Camera Exception:" + e,
                            Toast.LENGTH_LONG).show();
                }
            }



            // openFileChooser for Android < 3.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooser(uploadMsg, "");
            }

            // openFileChooser for other Android versions
            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                    String acceptType, String capture) {
                openFileChooser(uploadMsg, acceptType);
            }

            /** Added code to clarify chooser. **/

            // The webPage has 2 filechoosers and will send a console message
            // informing what action to perform, taking a photo or updating the
            // file
            public boolean onConsoleMessage(ConsoleMessage cm) {
                onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
                return true;
            }

            public void onConsoleMessage(String message, int lineNumber,
                    String sourceID) {
                // Log.d("androidruntime", "Per cÔøΩnsola: " + message);
            }
            /** Added code to clarify chooser. **/

        });

我只添加了一段代码。请询问我是否需要任何详细信息。

【问题讨论】:

  • 可以显示logcat错误吗?
  • 错误日志中没有任何内容。一旦点击文件选择器按钮然后没有动作触发..
  • 然后看看这里发布的链接:stackoverflow.com/questions/13448731/…
  • 我的代码和这个链接里有什么关系?
  • 我认为这是在某些三星设备中实现 Android 的问题。

标签: android android-webview


【解决方案1】:

在 Android 4.4 中似乎有一个 known bug 使 &lt;input type="file"&gt; 在 WebView 中不起作用。

很遗憾,openFileChooser 不是公共 API,也没有公共支持。 为此,在 Android Lollipop 中引入了 API onShowFileChooser

我的建议是使用JavaScript native WebView interface 解决该问题,并定义一个由应用程序的本机部分处理的自定义 openFileChooser API。

【讨论】:

  • 如果我尝试使用 javascript 原生 webview 界面,那么如何获取此方法的输入 public void openFileChooser(ValueCallback uploadMsg, String acceptType) {?
  • 这个想法是通过以下方式完全解决标准文件选择器行为: 1:在 Web 部件上有一个按钮,调用注入的 javascript API“openFile”。 2:通过启动获取文件的意图来处理本机部分的 openFile。 3:在native parte处理onActivityResult解析文件选择的路径。 4:通过自定义 JavaScript API 将路径发送到 Web 部件(例如 webView.evaluateJavaScript("selectedFile("path") ", null); 5:在 Web 部件上处理所选文件的逻辑。您可能需要更改一些 WebView 设置为 WebSettings.setAllowUniversalAccessFromFileURLs
  • 您好,您有什么解决方案吗?我对三星标签 3 也有同样的问题。如果您有解决方案,请发布您的答案
猜你喜欢
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2021-08-08
  • 1970-01-01
相关资源
最近更新 更多