这是我的WebViewActivity.java:
package com.laundrylang.newpro.activity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.laundrylang.newpro.R;
import com.laundrylang.newpro.constant.ConstantValue;
/**
* Created by Sinaan on 2016/4/5.
* 只要传入一个web网址(webUrl),就显示外链网址内容
*/
public class WebActivity extends Activity {
/**WebView显示区域*/
private WebView webView;
/**分享标题*/
private String title = "";
/**分享链接*/
private String url = "";
private static final int FILE_SELECT_CODE = 0;
private ValueCallback<Uri> mUploadMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_web);
url = getIntent().getStringExtra("webUrl");
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl(url);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new MyWebChromeClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode== KeyEvent.KEYCODE_BACK&& webView.canGoBack()) {
webView.goBack();
return true;
}else{
finish();
}
return super.onKeyDown(keyCode, event);
}
private class MyWebChromeClient extends WebChromeClient {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
WebActivity.this.title = title;
Log.e(ConstantValue.LogTag,"webView_title=="+title);
}
// 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/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILE_SELECT_CODE);
}
// 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("*/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILE_SELECT_CODE);
}
// 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/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILE_SELECT_CODE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case FILE_SELECT_CODE : {
Uri uri = data.getData();
Log.e("Tag", "Path:" + uri.toString());
mUploadMessage.onReceiveValue(uri);
mUploadMessage = null;
}
break;
}
}
}
主要就是重写openFileChooser方法,支持Android4.4。