我创建了一个WebView,它显示带有输入类型文件的网页。但是在选择文件后,我的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;
}
}
答案 0 :(得分:0)
尝试实现onActivityResult。重要的是调用onReceiveValue()方法,你可以用githhub中的一个例子来测试它 https://github.com/henrychuangtw/Kitkat-WebView。这是ActivityResult的一个例子: private ValueCallback mFIlePathCallback; private ValueCallback mFIlePathCallbackLollipop;
"web": "Microsoft.AspNet.Server.Kestrel --server.urls=http://*:5000"
答案 1 :(得分:0)
尝试此示例使用新版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);
} }