Android webview输入类型文件 [英] Android webview input type file

查看:122
本文介绍了Android webview输入类型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Comp.java

  package com.gururaju.bbmp; 

导入android.app.Activity;
导入android.os.Bundle;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
导入android.webkit.WebChromeClient;

公共类Comp扩展活动{
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);


$ b code
$ b activity_comp.xml

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

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

< / WebView>
< / LinearLayout>

comp.html (在资产文件夹中)

 <!DOCTYPE html> 
< html lang =en>
< head>
< link rel =stylesheettype =text / csshref =comp.css>
< meta charset =UTF-8>
< title>< / title>
< / head>
< body>
< h2 align =center>在此发布您的投诉< / h2>
< form enctype =multipart / form-dataaction =name =complaintsmethod =POST>
< input class =titletype =textname =titleplaceholder =Enter the Complaint Title/>< br />

< div class =spacer-welcome>< / div>
< textarea name =descclass =descplaceholder =您的投诉描述在这里...>< / textarea>< br />
< div class =spacer-welcome1>< / div>

< input id =centertype =filename =image>< br />
< input class =uploadtype =submitname =submitvalue =Submit>
< / form>
< / body>
< / html>

任何帮助将不胜感激。

解决方案

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

总共有四个隐藏的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)

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



https://github.com/delight-im/Android-AdvancedWebView 另外,你可以看看源代码,看看它是如何完成的:

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

I'm trying to build web project using android, from webview. I have a input field of type file <input type="file" > to let user upload files to server, but it seem not to work on android webview, when I tap on the browse button, nothing happens.

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");

    }
}

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>

comp.html (in assets folder)

<!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>

Any help would be appreciated.

解决方案

The answer by Riad points into the right direction, but that single callback is not enough to implement.

There are, in total, four hidden API methods that you have to implement. Their usage depends on the Android version. These methods are:

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)

You can use the following library which does all these things for you:

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

Alternatively, you may take a look at the source code to see how it's done:

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

这篇关于Android webview输入类型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆