Android webview输入类型文件

Gur*_*enx 5 html java android http webview

我正在尝试使用android从webview构建web项目.我有一个类型文件的输入字段,<input type="file" >让用户上传文件到服务器,但似乎不能在Android webview上工作,当我点击浏览按钮时,没有任何反应.

Comp.java

package com.gururaju.bbmp;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;

public class Comp extends Activity {
    WebView comp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comp);

        WebView myWebView = (WebView) findViewById(R.id.comp);
        myWebView.setWebChromeClient(new WebChromeClient());
        myWebView.loadUrl("file:///android_asset/comp.html");

    }
}
Run Code Online (Sandbox Code Playgroud)

activity_comp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/comp"
        >

        </WebView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

comp.html(在assets文件夹中)

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="comp.css">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h2 align="center">Post your Complaints here</h2>
    <form enctype="multipart/form-data" action="" name="complaints" method="POST">
        <input class="title" type="text" name="title" placeholder="Enter the Complaint Title" /><br />

        <div class="spacer-welcome"></div>
        <textarea name="desc" class="desc" placeholder="Your complaint description here..."></textarea><br />
        <div class="spacer-welcome1"></div>

            <input id="center" type="file" name="image" ><br />
        <input class="upload" type="submit" name="submit" value="Submit" >
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

caw*_*caw 2

Riad 的答案指出了正确的方向,但单个回调不足以实现。

您必须实现总共四种隐藏的 API 方法。它们的使用取决于 Android 版本。这些方法是:

public void openFileChooser(ValueCallback<Uri> uploadMsg)
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
Run Code Online (Sandbox Code Playgroud)

您可以使用以下库来为您完成所有这些操作:

https://github.com/delight-im/Android-AdvancedWebView

或者,您可以查看源代码以了解它是如何完成的:

https://github.com/delight-im/Android-AdvancedWebView/blob/master/Source/src/im/delight/android/webview/AdvancedWebView.java