【问题标题】:Android Java Upload File via WebView without FileChooserAndroid Java 通过 WebView 上传文件而不使用 FileChooser
【发布时间】:2019-12-16 22:05:48
【问题描述】:

我需要编写一个能够通过 WebView 加载网站的 Android 应用程序。该网站包含一个输入 (Type=FILE)

<form action="...">
    <input type="file" name="myFile" value="" id="..."><
    <input type="submit" value="submit"><
</form>

加载完成后,应用程序应使用特定的图像并通过存储路径上传:

String Path = "/storage/emulated/0/myimage.jpg"

我已经尝试打开 FileChooser-Dialog 并且可以正常工作,但我需要一个没有文件选择器的解决方案。应该使用“路径”变量的路径。我知道这将是一个巨大的安全漏洞,但是否有解决方案?也许通过 JavaScript 改变输入值?也许有图书馆?

PS:无意做任何违法的事情 - 公司应用正在生成个人资料图片,它们需要通过现有的不可更改的文件类型输入上传。

提前致谢。

【问题讨论】:

    标签: java android html webview upload


    【解决方案1】:

    你可以试试这个:

    1) 获取文件的 URI。


    2) 触发file chooser


    3) 像这样覆盖onShowFileChooser 内的WebChromeClient

    ValueCallback<Uri[]> mFilePathCallback;
    @Override
     public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
    
                  if (mFilePathCallback != null) {
                        mFilePathCallback.onReceiveValue(null);
                    }
                    mFilePathCallback = filePathCallback;
                    mFilePathCallback.onReceiveValue(myURI);
                    mFilePathCallback = null;
    
                    return true;
    
    }
    

    这里的myURI 是您文件的URI

    【讨论】:

    • 在尝试之前确保您拥有存储权限。
    【解决方案2】:

    检查我的答案here。您基本上需要做的是覆盖WebChromeClient 及其方法openFileChooser

    class MyWebChromeClient extends WebChromeClient {
        // For 3.0+ Devices (Start)
        // onActivityResult attached before constructor
        protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }
    
    
        // For Lollipop 5.0+ Devices
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            if (uploadMessage != null) {
                uploadMessage.onReceiveValue(null);
                uploadMessage = null;
            }
    
            uploadMessage = filePathCallback;
    
            Intent intent = fileChooserParams.createIntent();
            try {
                startActivityForResult(intent, REQUEST_SELECT_FILE);
            } catch (ActivityNotFoundException e) {
                uploadMessage = null;
                Toast.makeText(WebLink.this, "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
                return false;
            }
            return true;
        }
    
        //For Android 4.1 only
        protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE);
        }
    
        protected void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }
    }
    

    【讨论】:

    • 感谢您的回答,但这会打开 FileChooser,对吗? - 我需要一个解决方案来从特定路径上传文件,而无需打开选择器。
    • @ManuelMaurer 是的,你是对的。但是你可以自定义它。并使用uploadMessage.onReceiveValue 方法以相同的方法返回结果,而不打算选择文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多