我创建了一个WebView,它显示带有Input类型文件的网页。但在选择文件后,我的webview不会对任何东西做出反应,只对webview之外的对象做出反应(刷新按钮)。我该怎么修?
问题发生在android 4.1.2 中
我的代码:
package com.gymcourses.qiteq.pai16;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 1000;
private final static int FILECHOOSER_RESULTCODE=1;
ProgressBar progressBar;
public WebView webView;
private ValueCallback<Uri> mUploadMessage;
ImageView refreshBtn;
private ValueCallback<Uri[]> mFilePathCallback;
private Uri mCapturedImageURI = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (isNetworkConnected()) Log.v("INTERNET: ", "true");
else Log.v("INTERNET: ", "false;");
if (isNetworkConnected()) {
refreshBtn = (ImageView) findViewById(R.id.refreshBtn);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
refreshBtn.setVisibility(View.INVISIBLE);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
webView = (WebView) findViewById(R.id.webView);
webView.setVisibility(View.INVISIBLE);
webView.loadUrl("http://something.com");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(false);
webView.getSettings().setSavePassword(false);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());
WebChromeClient webChromeClient = new WebChromeClient(){
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
// callback.invoke(String origin, boolean allow, boolean remember);
callback.invoke(origin, true, false);
}
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("*/*");
MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);
}
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams){
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);
return false;
}
};
WebViewClient webViewClient = new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url){
progressBar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
refreshBtn.setVisibility(View.VISIBLE);
}
};
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(webViewClient);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.alertDialogMessage))
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
public void refresh(View view){
webView.reload();
refreshBtn.setVisibility(View.INVISIBLE);
webView.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
}
public boolean isNetworkConnected(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
}
尝试此示例使其适应API的新版本,检查FileChooser的新实现。
public class WebViewActivity extends ActionBarActivity {
private final static int FILECHOOSER_RESULTCODE = 1;
private final static String url = "https://www.cs.tut.fi/~jkorpela/forms/file.html";
public static WebViewActivity _activity;
ProgressDialog progressBar;
ProgressBar progressBar1;
AlertDialog alertDialog;
boolean loadingFinished = true;
boolean redirect = false;
Menu menu;
WebView wv;
private ValueCallback<Uri> mUploadMessage;
private String TAG = "WebViewActivity";
public static boolean checkInternetConnection(Activity _activity) {
ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
return conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_activity = this;
wv = new WebView(this);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setSaveFormData(true);
settings.setSavePassword(true); // Not needed for API level 18 or greater (deprecated)
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if (cookieManager.hasCookies()) {
Log.d(TAG, "has cookies");
} else {
Log.d(TAG, "has not cookies");
}
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
wv.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
wv.setWebChromeClient(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) {
WebViewActivity.this.showAttachmentDialog(uploadMsg);
}
// For Android > 3.x
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
WebViewActivity.this.showAttachmentDialog(uploadMsg);
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
WebViewActivity.this.showAttachmentDialog(uploadMsg);
}
});
this.setContentView(wv);
if (checkInternetConnection(_activity) == true) {
if (savedInstanceState == null) {
wv.loadUrl(url );
} else
wv.loadUrl(url );
alertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK).create();
alertDialog.setCancelable(true);
progressBar = ProgressDialog.show(WebViewActivity.this, getResources().getString(R.string.patientez),
getResources().getString(R.string.chargement));
progressBar.setCancelable(true);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
wv.loadUrl(urlNewString);
return true;
}
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "onPageFinished");
//CookieSyncManager.getInstance().sync();
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
//HIDE LOADING IT HAS FINISHED
if (progressBar != null && progressBar.isShowing()) {
progressBar.hide();
}
} else {
redirect = false;
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loadingFinished = false;
progressBar.show();
}
});
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
builder.setTitle(R.string.configDoc);
builder.setMessage(R.string.pro_refresh)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getApplicationContext(), ProActivity.class);
//intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
// to know if upload is finished
private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
this.mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
//if Back key pressed and webview can navigate to previous page
wv.goBack();
// go back to previous page
return true;
} else {
finish();
// finish the activity
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
// Do Some thing Here
// this.finish();
super.onBackPressed();
}
@Override
protected void onDestroy() {
super.onDestroy(); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
protected void onResume() {
super.onResume(); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
protected void onPause() {
super.onPause(); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
protected void onStop() {
super.onStop(); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
} }
尝试实现onActivityResult。重要的是onReceiveValue()方法的调用,您可以用githhub中的一个例子来测试https://github.com/henrychuangtw/Kitkat-WebView.以下是ActivityResult的示例:private ValueCallback mFIlePathCallback;私有价值回调mFIlePathCallbackLollipop;
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.d(TAG, "onActivityResult");
if ((requestCode == mFilechooserResultcode) && (resultCode == RESULT_OK)) {
if ((mFIlePathCallback == null) && (mFIlePathCallbackLollipop == null)) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//lollipop +
String dataString = intent.getDataString();
if (dataString != null) {
Uri[] result = new Uri[]{Uri.parse(dataString)};
mFIlePathCallbackLollipop.onReceiveValue(result);
mFIlePathCallbackLollipop = null;
}
} else {
//kitkat -
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mFIlePathCallback.onReceiveValue(result);
mFIlePathCallback = null;
}
}
}