1、重写WebChromeClient,完成代码如下:
package com.cxwl.nst.supplyinfor.activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Message;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
abstract class TestWebChromeClient extends WebChromeClient {
private WebChromeClient mWrappedClient;
protected TestWebChromeClient(WebChromeClient wrappedClient) {
mWrappedClient = wrappedClient;
}
/** } */
@Override
public void onProgressChanged(WebView view, int newProgress) {
mWrappedClient.onProgressChanged(view, newProgress);
}
/** } */
@Override
public void onReceivedTitle(WebView view, String title) {
mWrappedClient.onReceivedTitle(view, title);
}
/** } */
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
mWrappedClient.onReceivedIcon(view, icon);
}
/** } */
@Override
public void onReceivedTouchIconUrl(WebView view, String url,
boolean precomposed) {
mWrappedClient.onReceivedTouchIconUrl(view, url, precomposed);
}
/** } */
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
mWrappedClient.onShowCustomView(view, callback);
}
/** } */
@Override
public void onHideCustomView() {
mWrappedClient.onHideCustomView();
}
/** } */
@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
return mWrappedClient.onCreateWindow(view, dialog, userGesture, resultMsg);
}
/** } */
@Override
public void onRequestFocus(WebView view) {
mWrappedClient.onRequestFocus(view);
}
/** } */
@Override
public void onCloseWindow(WebView window) {
mWrappedClient.onCloseWindow(window);
}
/** } */
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return mWrappedClient.onJsAlert(view, url, message, result);
}
/** } */
@Override
public boolean onJsConfirm(WebView view, String url, String message,
JsResult result) {
return mWrappedClient.onJsConfirm(view, url, message, result);
}
/** } */
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
return mWrappedClient.onJsPrompt(view, url, message, defaultValue, result);
}
/** } */
@Override
public boolean onJsBeforeUnload(WebView view, String url, String message,
JsResult result) {
return mWrappedClient.onJsBeforeUnload(view, url, message, result);
}
/** } */
@Override
public void onExceededDatabaseQuota(String url, String databaseIdentifier,
long currentQuota, long estimatedSize, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater) {
mWrappedClient.onExceededDatabaseQuota(url, databaseIdentifier, currentQuota,
estimatedSize, totalUsedQuota, quotaUpdater);
}
/** } */
@Override
public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater) {
mWrappedClient.onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, quotaUpdater);
}
/** } */
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
mWrappedClient.onGeolocationPermissionsShowPrompt(origin, callback);
}
/** } */
@Override
public void onGeolocationPermissionsHidePrompt() {
mWrappedClient.onGeolocationPermissionsHidePrompt();
}
/** } */
@Override
public boolean onJsTimeout() {
return mWrappedClient.onJsTimeout();
}
/** } */
@Override
@Deprecated
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
mWrappedClient.onConsoleMessage(message, lineNumber, sourceID);
}
/** } */
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return mWrappedClient.onConsoleMessage(consoleMessage);
}
/** } */
@Override
public Bitmap getDefaultVideoPoster() {
return mWrappedClient.getDefaultVideoPoster();
}
/** } */
@Override
public View getVideoLoadingProgressView() {
return mWrappedClient.getVideoLoadingProgressView();
}
/** } */
@Override
public void getVisitedHistory(ValueCallback<String[]> callback) {
mWrappedClient.getVisitedHistory(callback);
}
/** } */
public void openFileChooser(ValueCallback<Uri> uploadFile) {
((TestWebChromeClient) mWrappedClient).openFileChooser(uploadFile);
}
}
2、activity页面引用
a、初始化 全局变量
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
b、onCreate中写如下代码
webView = (WebView) findViewById(R.id.supplyinfor_news_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setAllowFileAccess(true);// 设置允许访问文件数据
webSettings.setJavaScriptEnabled(true);// 设置支持javascript脚本
webSettings.setBuiltInZoomControls(true);// 设置支持缩放
webView.getSettings().setBuiltInZoomControls(true);//启用触摸放大
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new TestWebChromeClient(new WebChromeClient()) {
// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
supplyinforList.this.startActivityForResult(Intent.createChooser(i, getString(R.string.uploadStr)), supplyinforList.FILECHOOSER_RESULTCODE);
}
});
c、创建onActivityResult方法
@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;
}
}
复制a,b,c三部分代码 基本上就可以实现upload上传了
----------------------------------------------------------------------------补充---------------------------------------------------------------------------
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); web = (WebView) findViewById(R.id.webview01); progressBar = (ProgressBar) findViewById(R.id.progressBar1); web = new WebView(this); web.getSettings().setJavaScriptEnabled(true); web.loadUrl("http://www.script-tutorials.com/demos/199/index.html"); 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/*"); MyWb.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("*/*"); MyWb.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/*"); MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE ); } }); setContentView(web); }