参考http://blog.csdn.net/zhtsuc/article/details/49154099
直接上代码
public class MainActivity1 extends Activity {
private WebView webView;
private WebSettings wvsetting;
private ImageView news_iv;
public ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> mUploadMessageForAndroid5;
public final static int FILECHOOSER_RESULTCODE = 1;
public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) this
.findViewById(R.id.news_content_webview);
webView = (WebView) this.findViewById(R.id.news_content_webview);
news_iv = (ImageView) this.findViewById(R.id.news_iv);
wvsetting = webView.getSettings();
// 设置是否允许通过file
// url加载的Javascript可以访问其他的源,包括其他的文件和http,https等其他的源。这个设置在JELLY_BEAN以前的版本默认是允许,在JELLY_BEAN及以后的版本中默认是禁止的
try {
if (Build.VERSION.SDK_INT >= 16) {
Class<?> clazz = webView.getSettings().getClass();
Method method = clazz.getMethod(
"setAllowUniversalAccessFromFileURLs", boolean.class);
if (method != null) {
method.invoke(webView.getSettings(), true);
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 内嵌图片时加载缓慢的问题
if (Build.VERSION.SDK_INT >= 19) {
wvsetting.setLoadsImagesAutomatically(true);
} else {
wvsetting.setLoadsImagesAutomatically(false);
}
// 屏幕适配滚动问题
wvsetting
.setUserAgentString("Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn;CmsTop Cloud Mobile) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Safari/537.36");
wvsetting.setJavaScriptEnabled(true);// 支持js
wvsetting.setSupportZoom(false);
wvsetting.setAllowFileAccess(true);
wvsetting.setDomStorageEnabled(true);
wvsetting.setSupportMultipleWindows(true);
wvsetting.setPluginState(WebSettings.PluginState.ON);
wvsetting.setLoadWithOverviewMode(true);
wvsetting.setUseWideViewPort(true);
wvsetting.setDefaultTextEncodingName("utf-8");
wvsetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
wvsetting.setAllowFileAccess(true);
webView.setWebChromeClient(new MyWebChromeClient());
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("https://jinshuju.net/f/oRYFvm");
}
public class MyWebChromeClient extends WebChromeClient {
//扩展浏览器上传文件
//3.0++版本
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
openFileChooserImpl(uploadMsg);
}
//3.0--版本
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooserImpl(uploadMsg);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooserImpl(uploadMsg);
}
// For Android > 5.0
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
openFileChooserImplForAndroid5(uploadMsg);
return true;
}
}
private void openFileChooserImpl(ValueCallback<Uri> 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<Uri[]> 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);
}
@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;
}
}
}