我一直在努力从WebView上传文件。我在谷歌上搜索并实施了所有建议的解决方案(例如这篇SO帖子),但没有一个可行。

我有一个HTML页面与以下代码上传文件。

<form method="POST" enctype="multipart/form-data">
  File to upload: <input type="file" name="uploadfile">&nbsp;&nbsp;
  <input type="submit" value="Press to Upload..."> to upload the file!
</form>

它在桌面浏览器如Firefox和内置浏览器中运行良好 的模拟器/ AVD,即,当我点击“浏览…”按钮渲染 元素,浏览器打开一个对话框 框,在那里我可以选择一个文件上传。

然而,在android 3.0模拟器/ AVD中,当我点击“选择” 文件”,什么都没有发生,没有文件对话框被打开!


当前回答

我发现有必要在Android 4.1中定义公共void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)。然后我采用了米歇尔·奥利弗的解决方案。

其他回答

蜂窝(API 11)到Android 11的工作方法

由swati vishnoi更新,这适用于派和以上

static WebView mWebView;
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 1;

修改onActivityResult ()

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        if (requestCode == REQUEST_SELECT_FILE)
        {
            if (uploadMessage == null)
                return;
            uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
            uploadMessage = null;
        }
    }
    else if (requestCode == FILECHOOSER_RESULTCODE)
    {
        if (null == mUploadMessage)
            return;
    // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
    // Use RESULT_OK only if you're implementing WebView inside an Activity
        Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
    else
        Toast.makeText(getActivity().getApplicationContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
}

现在在onCreate()或onCreateView()中粘贴以下代码

    WebSettings mWebSettings = mWebView.getSettings();
    mWebSettings.setJavaScriptEnabled(true);
    mWebSettings.setSupportZoom(false);
    mWebSettings.setAllowFileAccess(true);
    mWebSettings.setAllowContentAccess(true);

mWebView.setWebChromeClient(new 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 Browser"), FILECHOOSER_RESULTCODE);
    }
   

    // For Lollipop 5.0+ Devices
    public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.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(getActivity().getApplicationContext(), "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 Browser"), 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);
    }
});

解决方案:https://github.com/delight-im/Android-AdvancedWebView


崩裂剂解决方案:

activity和fragment的区别只在onActivityResult中:

片段:

lateinit var webViewGlobal: AdvancedWebView private set

class WebViewFragment : Fragment(), AdvancedWebView.Listener {

private lateinit var binding: FragmentWebViewBinding

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    binding = FragmentWebViewBinding.inflate(inflater)
    webViewGlobal = binding.webWiew
    return binding.root
    }
}

活动:

class MainActivity : AppCompatActivity() {
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        webViewGlobal.onActivityResult(requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)
    }
}

Android 11:

<application
       ...
       android:requestLegacyExternalStorage="true"
       ...
/>

清单:

 <uses-permission android:name="android.permission.CAMERA"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我发现有必要在Android 4.1中定义公共void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)。然后我采用了米歇尔·奥利弗的解决方案。

我花了将近一个月的时间研究这个问题。一切都失败了。在多个网站中看到的所有代码都不工作。但这里存在最好的解决方案

https://github.com/chiclaim/android-webview-upload-file

步骤

1)点击克隆或下载

2)在本地目录中获取zip fle

3)解压压缩文件

4)开放Android工作室

5)转到文件---->打开>导航到你解压内容的目录。

6)在webView中更改所需的web url。loadUrl(“您的url hre”);在MainActivity.java

7)适用于Android studio 3.4.2版本

你访问过这个链接吗? http://groups.google.com/group/android-developers/browse_thread/thread/dcaf8b2fdd8a90c4/62d5e2ffef31ebdb

http://moazzam-khan.com/blog/?tag=android-upload-file

http://evgenyg.wordpress.com/2010/05/01/uploading-files-multipart-post-apache/

通过javalib Apache Commons上传文件的简明示例

我想你会从中得到帮助的