Webview's loadData() is not working in android 10.0 (Q) - android

Here i am trying to load Html code as string in webview's loadData() .Nothing is happen over this mehtod but same method is working like charm in below sdk 29.
webview.loadData(html_code,"text/html",null);
Note : Here i am not performing any encoding or decoding operation on string.I am simply passing it as string to above method.

Use this code, it will work.
String newhtml_code = Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING);
testWebView.loadData(newhtml_code,"text/html", "base64");

Try calling
String encodedHtml = Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING);
webview.getSettings().setJavaScriptEnabled(true);
before
webview.loadData(encodedHtml , "text/html", "base64");
like below
String html_code= "<html><body>Your Actualtext.</body></html>";
String encodedHtml = Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadData(encodedHtml , "text/html", "base64");
for more details refer to this link

manifest file in
android:usesCleartextTraffic="true"
and
WebSettings settings = wb_webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
String html_code = "html code";
wb_webview.loadData(Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING) , "text/html", "base64");

Now it is working after performing base-64 encoding to string html_code.
Issue resolved by passing html_code string as per given instruction in docs

I came up with another solution by using loadDataWithBaseURL
e.g.
webView.loadDataWithBaseURL(null, html, "text/html", null, null)
It should use less CPU and memory resource as no need Base64 calculation and storing.

I facing the same issue and fix it by using loadDataWithBaseURL() instead loadData() method
mWebView.loadData(mHtml, "text/html", "UTF-8");
solution:
mWebView.loadDataWithBaseURL(null,mHtml,"text/html", "UTF-8", null);

Related

Android: How to convert local string(html) to html page and display in the Webview

The string contains the html page code. As the requirement, it better not to store in the file. How to represent it in the Webview?
Hi if you string contains the html page code then you can directly load in Webview like below.
webView.loadData(webData, "text/html; charset=UTF-8", null);
Try this:
WebView wv = (WebView)this.findViewById(R.id.myWebView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, /*Your HTML string*/, "text/html", "utf-8", null);
Check out this: http://developer.android.com/reference/android/webkit/WebView.html
String summary = "<html><body>Your <b>HTML</b> code.</body></html>";
webview.loadData(summary, "text/html", null);
for more reference check this answer..
https://stackoverflow.com/a/14002475/5362535

WebView shows source html with loadDataWithBaseURL, not rendered view

I'm developing an application witch uses WebView to render custom html.
But when I call
loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", "text/html; charset=utf-8;", "utf-8", null);
it shows html itself (not rendered one) on Genymotion emulator.
On my HTC-one, it works fine with rendered html.
Each result is showed as attached.
Does anyone have a same problem or solution?
Thanks.
Don't enter mimeType below KitKat.
fun getMimeType(): String? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
"text/html; charset=utf-8"
} else {
null
}
}
loadDAtaWithBaseURL(URL, "<html><h1>TEST</h1></html>", getMimeType(), "utf-8", null);
Java:
if(Build.VERSION.SDK_INT < 21)
webView.loadDataWithBaseURL("about:blank","<html><h1>TEST</h1></html>","text/html", "UTF-8",null);
else
webView.loadDataWithBaseURL("about:blank","<html><h1>TEST</h1></html>","text/html; charset=utf-8", "UTF-8",null);
Regarding the info you have given, i can not have a clear debug for the issue, but this is how it should be done, just to check if you missed something
First, add this line to your activity in the manifest file
Load your data using
public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl);
And this is done this way
loadDataWithBaseURL(Url, data, "text/html", "UTF-8", historyUrl)
Note that
If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored, and the data will be treated as part of a data: URL. If the base URL uses any other scheme, then the data will be loaded into the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded entities in the string will not be decoded.

WebView is not using Cookies for loading images

I have troubles when I want my WebView to load images that requires Cookies.
I have set my cookies on the 'CookieManager'
final android.webkit.CookieManager instance = android.webkit.CookieManager.getInstance();
instance.setAcceptCookie(true);
instance.setCookie(".example.fr", mCookies, new ValueCallback<Boolean>() {
#Override
public void onReceiveValue(final Boolean value) {
loadWebView();
}
});
The WebView is then loaded with a custom HTML string because the app is generating the proper HTML.
private void loadWebView() {
// this string is an example of a generated HTML
String htmlContent =
"<!DOCTYPE html>" +
"<html><head>" +
"<link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\" media=\"screen, projection\"/></head>" +
"<body><img src=\"www.example.fr/img.jpg\"/></body></html>";
mWebView.loadDataWithBaseUrl("file:///android_asset/", htmlContent, "text/html", "UTF-8", null);
}
I tried to proxy the network calls with Charles Proxy and I noticed that the request to www.example.fr/img.jpg had no cookie set in the headers. But, when I inspect the WebView using Chrome debbuging, I can see that the Cookies are properly under the Resources tab.
It seems that they are not used for the image downloading.
Any hints or advices to make the WebView using Cookies for resource downloading ?
Thank you.
I've faced with same problem, it is related with following changes:
https://developer.android.com/about/versions/android-5.0-changes.html#BehaviorWebView
to fix it you need set:
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);

How to avoid unnecessary characters in WebView

I am setting String in WebView in Android, but i am getting such characters http://img4.imageshack.us/img4/5200/hkgm.jpg. How to avoid them ? IF i put a log and see then it will be proper but only in WebView it is not working properly
Thanks in advance
Code :
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String summary = rssStr.get(position).getEncodedContent();
Log.d("String", summary);
web.loadData(String.format(text, summary), "text/html", "utf-8");
By setting the Encoding type in the Webview can resolve this issue,
wv.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);

Directly put html code in a WebView (Android)

While using WebView, we usually put a URL for it:
WebView.loadUrl(myURL);
but it is possible to put HTML code directly?? So that it will be in a logic that:
WebView.loadContent ( <html><head><script></script></head><body>....</body></html> );
Thanks.
Check out this:
http://developer.android.com/reference/android/webkit/WebView.html
// OR, you can also load from an HTML string:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
String yourhtmlpage = "<html><body>You scored <b>hello world</b> points.</body></html>";
webview.loadDataWithBaseURL(null, yourhtmlpage, "text/html", "UTF-8", null);
Try this code. It works for me.
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);

Categories

Resources