我正在尝试在Android WebView上加载由Wordpress提供支持的博客。而内容和标题'图像正在显示,帖子内的图像(我的意思是从显示的网页打开帖子)不会显示(所有文本甚至广告都会显示)。仅在网络浏览器上查看网站并不会显示此类问题。我尝试过在这个社区就类似问题提出的各种其他问题中建议的方法,但未能解决我的问题。
这是代码
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.cuous_web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(this.getApplicationContext().
getCacheDir().getAbsolutePath());
webSettings.setLoadsImagesAutomatically(true);
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setSupportZoom(true);
webSettings.setUserAgentString("Android WebView");
mixedContentIfRequired(webSettings);
webSettings.setBuiltInZoomControls(true);
webView.setWebViewClient(new CuousViewClient());
webView.setWebChromeClient(new CuousChromeClient());
webView.loadUrl("http://cuous.com/");
}
private void mixedContentIfRequired(WebSettings webSettings) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.
MIXED_CONTENT_COMPATIBILITY_MODE);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
if(mProgress != null && mProgress.isShowing())
mProgress.dismiss();
super.onDestroy();
}
private class CuousChromeClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int progress) {
if (mProgress == null) {
mProgress = new ProgressDialog(MainActivity.this);
mProgress.show();
mProgress.setCanceledOnTouchOutside(false);
}
mProgress.setMessage("Loading " + String.valueOf(progress) + "%");
if (progress == 100) {
mProgress.dismiss();
mProgress = null;
}
}
}
private class CuousViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
ActionBar actionBar = getSupportActionBar();
if(actionBar.isShowing()) {
actionBar.hide();
}
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer);");
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if(handler != null) {
handler.proceed();
}
else super.onReceivedSslError(view, null, error);
}
}
}
答案 0 :(得分:0)
您好我尝试了与我的代码链接。我可以看到图片:see the photo。这是你想要的吗?
那是我的主要活动:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("file:///android_asset/www/index.html"); //try js alert
view.setWebChromeClient(new WebChromeClient()); // adding js alert support
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:") || url.startsWith("market:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
else {
view.loadUrl(url);
return true;
}
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
view.goBack(); //method goback()
return true;
}
return super.onKeyDown(keyCode, event);
}
}
还可以问一下最低SDK版本是什么?
修改强>
<强> UPDATE_SHOULDURLOVERRIDING 强>
这是一段代码,它将执行其他操作然后只需打开webview中的url它控制它就像mailto .... 我对mailto的意思是: 在HTML中你可以这样做:myname@gmail.com并在你的电脑上它将打开你的前景。如果你有android并且那段代码不存在则会出错。如果你有这段代码,它会在mailserivce中询问我是否需要打开它。
修改强> 所以,如果你通过这种方法加载它可以吗?
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
而不是:
view.loadUrl("file:///android_asset/www/index.html"); //try js alert