Android webview输入类型文件
Android webview input type file
我正在尝试使用 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");
}
}
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(在资产文件夹中)
<!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>
如有任何帮助,我们将不胜感激。
尝试像这样实现 file Chooser
方法,如最后提供的文章 link 中所述:
webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
// Update message
mUploadMessage = uploadMsg;
try{
// do work....
}catch(Exception e){
Toast.makeText(getBaseContext(), "Exception:"+e,
Toast.LENGTH_LONG).show();
}
}
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)
您可以使用以下库为您完成所有这些事情:
https://github.com/delight-im/Android-AdvancedWebView
或者,您可以查看源代码以了解它是如何完成的:
Webview class单独不支持在facebook上上传文件。您需要同时使用 web chrome 客户端和 webview 客户端来处理 android 应用程序上的文件上传,如下所示。 View more details and a working demo
package com.whatsonline.androidphotouploadonfacebook;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
/**
* Created by sada on 6/17/2016.
*/
public class upload extends Activity {
WebView web;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
LinearLayout ln1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
web = new WebView(this);
web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
ln1=(LinearLayout) findViewById(R.id.ln1);
WebSettings settings=web.getSettings();
settings.setJavaScriptEnabled(true);
web.loadUrl("http://www.facebook.com");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient() {
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
upload.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), upload.FILECHOOSER_RESULTCODE);
}
});
ln1.addView(web);
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE){
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
}
我正在尝试使用 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");
}
}
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(在资产文件夹中)
<!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>
如有任何帮助,我们将不胜感激。
尝试像这样实现 file Chooser
方法,如最后提供的文章 link 中所述:
webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
// Update message
mUploadMessage = uploadMsg;
try{
// do work....
}catch(Exception e){
Toast.makeText(getBaseContext(), "Exception:"+e,
Toast.LENGTH_LONG).show();
}
}
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)
您可以使用以下库为您完成所有这些事情:
https://github.com/delight-im/Android-AdvancedWebView
或者,您可以查看源代码以了解它是如何完成的:
Webview class单独不支持在facebook上上传文件。您需要同时使用 web chrome 客户端和 webview 客户端来处理 android 应用程序上的文件上传,如下所示。 View more details and a working demo
package com.whatsonline.androidphotouploadonfacebook;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
/**
* Created by sada on 6/17/2016.
*/
public class upload extends Activity {
WebView web;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
LinearLayout ln1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
web = new WebView(this);
web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
ln1=(LinearLayout) findViewById(R.id.ln1);
WebSettings settings=web.getSettings();
settings.setJavaScriptEnabled(true);
web.loadUrl("http://www.facebook.com");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient() {
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
upload.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), upload.FILECHOOSER_RESULTCODE);
}
});
ln1.addView(web);
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE){
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
}