Images not loading in android webview - android

I am trying to load the site in an android app web view.
The site loads without the images ,all the images from the site are not loaded what could be the problem.
The code for onCreate is shown below.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
String url = getResources().getString(R.string.web_url);
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setUseWideViewPort(true);
web.loadUrl(url);
}
One more note, when i set javascript to false using web.getSettings().setJavaScriptEnabled(false); the images load giving a warning that javascript should be enabled to run the site properly.
This site is protected with CloudFlare ,could this be the reason images are not loaded in the android web view ?

Add
web.getSettings().setDomStorageEnabled(true);
This should work. Keep the rest as is.
Although a caveat, I don't think its recommended to enable this by default, its disabled by default. The setting allows a website to store data and reuse it. So the images are not being loaded, because this site wasn't allowed to store them on your device. This opens up a can of security concerns.

Just use this line of code. I think your problem will be solved.
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
You may need this one as well, if you are lazy-loading content:
webView.getSettings().setJavaScriptEnabled(true);

Just this will suffice
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

I had similar problem in Android 9.0. The images in the site's html were using http instead of https.
Then I changed all the http with https and everything worked!
It was very easy to change the http to https using a sql query in mySql.
I am giving the query if it helps anyone!
UPDATE table_name SET column_name = replace(column_name, '<img src="http://', '<img src="https://')

I suspect that the issue might be your webview client.
Use this instead:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(FundCardWeb.this, "Page Load Error" + description, Toast.LENGTH_SHORT).show();
}
});

for me the combination of adding those did it:
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
I don't like adding unneccesery cod when so I didn't use:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

I have faced the same issue. In the web view, the Image with the http URL was not loading in my app. The following solution fixed my issue,
webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
But in Android 9, the above solution has thrown the issue sometimes, in such case add the following line in the manifest file.
android:usesCleartextTraffic="true"

My problem was just one image, not loading in webview. I just add this two thing
In Manifest
android:usesCleartextTraffic="true"
In java oncreate() just add
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mywebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

Related

Android - Image Loading from WebPage in WebView

I am trying to load a wiki web page into a native Android WebView with WebView.loadUrl(String url). It works and displays all of the web content, except for the images. However, I have not found a reason for this. Does anyone know why image resources would not load automatically? Is there a method I need to overwrite or a parameter that needs to be set? I can't find anything in the documentation - it looks like the default settings should allow for automatic image loading. Any information would be appreciated. Thanks!
mWebView = (WebView)findViewById(R.id.webView);
setWebViewClient(mWebView);
setWebSettings(mWebView);
mWebView.loadUrl("http://my_wiki_url_goes_here/");
private void setWebViewClient(WebView mWebView) {
WebViewClient client = new WikiWebViewClient();
mWebView.setWebViewClient(client);
}
private void setWebSettings(WebView mWebView) {
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
}
public class WikiWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
Printing the url onLoadResource(), I see that the image urls generally start with "data:image/png;base64,". I don't think that should makes a difference or not - it works in the web browser.
Huh, looks like simply enabling DOM storage did the trick.
settings.setDomStorageEnabled(true);
If I don't receive a better explanation or solution, I will accept this answer once I am able to

Android WebView loading part of a website

Some websites have comments portion and many other unnecessary portions. I want to just display the main article excluding the other portions in my custom Android WebView.
I have tried the example from Display a part of the webpage on the webview android
But it still shows the full website.
There is also the loaddatawithbaseurl class in Android but it is not working too as you can't specify till which portion of a website to show using this class.
Is there any other way to do this?
Here it is with some changes...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
String javaScript = "javascript: var form = document.getElementsByClassName('university_corner');"
+ "var body = document.getElementsByTagName('div');"
+ "body[0].innerHTML = form[0].innerHTML";
webView.loadUrl(javaScript);
}
});
webView.loadUrl(url);

setUserAgentString in Android webview has no effect on HTTP header used in loadURL()

Been trying to change the User-Agent string in the HTTP request of an Android app. I have tested this together with wireshark and the emulator, and have seen that although I set the useragent string in the webview, the associated loadUrl request does not use this user-agent string. Instead I see the Dalvik useragent string in the wireshark capture.
Here is the code abstract. Any ideas? Or does the emulator not support this?
#Override
public void run() {
assert(context != null);
...
...
webView = new WebView(context);
...
String defaultUserAgent = "betaUAteststring";
// Clear per-application caches etc
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
....
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
..
}
#Override
public void onLoadResource(WebView view, String url) {
...
}
});
// Start loading
webView.getSettings().setUserAgentString(defaultUserAgent);
String setUA = webView.getSettings().getUserAgentString();
//--> logging here shows the correct user agent, so the webview does accept the value
// However the following statement does not result in an http request with the webviews user agent
webView.loadUrl(url);
//Alternative doesn't help either (and shouldn't according to javadoc)
//Map<String,String> headerMap = new HashMap<String,String>();
//headerMap.put("User-Agent","uaTestInAMap");
//webView.loadUrl(url, headerMap);
}
Answering my own question. It appears that the emulator for whatever reason is not taking the user agent string from the webview. I have not found out the reason for this however.
The code works fine on a real device.
You miss to override the default Android behavior on open url (launch default browser).
To use your customize browser to navitage, you only have to attach a WebViewClient to WebView
That is achieve adding the following line to your code:
webView.setWebViewClient(new WebViewClient());
Cheers,
Rodrigo

Getting blank page in Android WebViewClient

I am trying open twitter link : http://mobile.twitter.com/pawan_rathore88
in my activity. If I set WebViewClient to webview I am getting blank page.
But when I load url without setting any webviewclient, it loads page properly.
Does anyone have idea what can be a problem. Following is my code snippet.
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
//if I comment the following line then webpage loads properly in default Android browser.
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v(tag, "url :" + url);
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://mobile.twitter.com/pawan_rathore88");
Thanks,
Pawan
After tweaking the code around, it seems to be a user agent problem, seems that changing it to a desktop useragent fixes this problem :
WebView web = (WebView)findViewById(R.id.webView1);
web.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; " +
"U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
"like Gecko) Version/5.0 Safari/533.16");
String url = "http://mobile.twitter.com/pawan_rathore88";
web.loadUrl(url);
This is a problem with twitter at the moment and fails in all web kit mobile browsers.
That is exact my problem too. Load Desktop version very good but not mobile version. If running on default browser, then Mobile version normally run. I think the problem is not well perform user string agent. But cannot find it out now.

"Hello, WebView" example

I'm new in android development and I am trying out the WebView example in the official android site.
http://developer.android.com/guide/tutorials/views/hello-webview.html
But I do everything they say...which is pretty simple: I create the project, edit the layout file, then I add the code, etc. No problems building...but when I launch the app in the simulator I just got a black screen. It is like if the Layout is empty...like if the WebView is not created.
What am I doing wrong?
Sorry about that – that link is a bit outdated. The fixed version of this tutorial is available here:
http://developer.android.com/guide/webapps/webview.html
We should remove the old link; I'll file a bug.
And note, the error is that setContentView isn't being called.
in oncreate method add WebView.enablePlatformNotifications();
in manifest file add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
now it works fine...
webview_id = (WebView)findViewById(R.id.webview_id);
webview_id.getSettings().setJavaScriptEnabled(true); // enable javascript
WebSettings webSettings = webview_id.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webview_id.setInitialScale(90);
webSettings.setLoadWithOverviewMode(true);
webview_id.requestFocusFromTouch();
webview_id.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Snackbar.with(getApplicationContext()).dismiss();
Snackbar.with(getApplicationContext()) // context
.text(description) // text to display
.show(MainActivity.this);
}
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
if(isNetworkAvailable()){
webview_id .loadUrl("http://helloworld.org/");
}else{
Snackbar.with(getApplicationContext()).dismiss();
Snackbar.with(getApplicationContext()) // context
.text("Please Check your Internet Connection") // text to display
.show(MainActivity.this);
progressBar.setVisibility(View.VISIBLE);
}
}

Categories

Resources