一、介绍
当我们在使用webview控件打开一个web网页时,如果we页面中带有的控件,在webview中能正常显示这个上传控件,但是你会发现无论你如何点击都无效果,这个是很让人恼火的,一时也不知道如何下手去改,这里阿汤哥会告诉你如何解决该问题,如果我的解决办法能帮到你,请给我点掌声,并给你自己点掌声。
二、解决办法
第一步:重写WebChromeClient
webview的坑比较多,在这个上传文件的坑中遇到一个问题:
Android 5.0+ 重写onShowFileChooser生效;
Android 4.4 重写openFileChooser没有生效;
Android 4.4- 重写openFileChooser生效;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
/**
* Created by tangbin on 16/5/12.
*/
public class MyWebChromeClient extends WebChromeClient {
private WebCall webCall;
public void setWebCall(WebCall webCall) {
this.webCall = webCall;
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
if (webCall != null)
webCall.fileChose(uploadMsg);
}
// For Android < 3.0
public void openFileChooser(ValueCallback uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android > 4.1.1
public void openFileChooser(ValueCallback uploadMsg,
String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
// For Android > 5.0
@Override
public boolean onShowFileChooser(WebView webView,
ValueCallback filePathCallback,
FileChooserParams fileChooserParams) {
if (webCall != null)
webCall.fileChose5(filePathCallback);
return super.onShowFileChooser(webView, filePathCallback,
fileChooserParams);
}
public interface WebCall {
void fileChose(ValueCallback uploadMsg);
void fileChose5(ValueCallback uploadMsg);
}
}
第二步:监听ValueCallback
WebSettings webSettings = mWebView.getSettings();
// 设置WebView属性,能够执行Javascript脚本
webSettings.setJavaScriptEnabled(true);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
mWebView.setWebViewClient(new webViewClient());
public final static int FILECHOOSER_RESULTCODE = 1;
public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
public ValueCallback mUploadMessage;
public ValueCallback mUploadMessageForAndroid5;
@Override
public void fileChose(ValueCallback uploadMsg) {
openFileChooserImpl(uploadMsg);
}
@Override
public void fileChose5(ValueCallback uploadMsg) {
openFileChooserImplForAndroid5(uploadMsg);
}
private void openFileChooserImpl(ValueCallback 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);
}
private void openFileChooserImplForAndroid5(ValueCallback uploadMsg) {
mUploadMessageForAndroid5 = uploadMsg;
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
startActivityForResult(chooserIntent,
FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
}
第三步:创建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;
} else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
if (null == mUploadMessageForAndroid5)
return;
Uri result = (intent == null || resultCode != RESULT_OK) ? null
: intent.getData();
if (result != null) {
mUploadMessageForAndroid5.onReceiveValue(new Uri[] { result });
} else {
mUploadMessageForAndroid5.onReceiveValue(new Uri[] {});
}
mUploadMessageForAndroid5 = null;
}
}