Online images in local Webview page in Android - android

I have a webview showing a local html page resource in a string like this:
<p>Hi.</p>
<p><img alt="" src="http://ADRESS.COM/image.jpg" style="height:300px; width:400px" /></p>
and doing that with this code:
WebView web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadDataWithBaseURL("",notice,"text/html","UTF-8","");
The page contains some images with online src. but in android WebView the images doesn't load. (in chrome it works).
What I should do?
thanks.

Add these two lines in your code.
web.getSettings().setLoadsImagesAutomatically(true);
web.getSettings().setDomStorageEnabled(true);
Instead of using web.loadDataWithBaseURL("",notice,"text/html","UTF-8",""); simply load your website url as follows
web.loadUrl("http://www.google.com");
if you want load your local html page. Create asset directory android then put your html file in asset folder and and set it as follows
web.loadUrl("file:///android_asset/yourFile.html");

Related

Android WebView open html page from WebView html

I am loading my android WebView using
mywebview.loadDataWithBaseURL("file:///android_asset/", new String(result), "text/html", "UTF-8", null);
HTML rendered successfully in WebView. now I want to open another HTML file from that HTML using button for that I am using below code in HTML.
<button onclick="location.href='data/1/quiz_adjective.html'" > Start</button>
but it does not work. my asset html file directory is - assets\data\1\htmlfile.html
When you make Android applications, you can parse HTML data or HTML pages got from the Web by JSoup libraly.
Before loading url you should enable JS like this. Then, It should work. Or there is a more advanced way with JavascriptInterfacedocs
mywebview.getSettings().setJavaScriptEnabled(true);

android - Loading HTML from database into WebView

so i'm trying to load some HTML from my sqlite database into a webview.
This works splendid as the html is rendered beautifully but here comes the problem. The local file added in the html is not being load. I added it to the assets folder in this hierarchy: assets/folder1/folder2/placeholder.png but i get a Not allowed to load local resource: error. How can i get past this please.
Please note that the data is being populated from the db.
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAllowFileAccess(true);
webView.loadData(data, "text/html", "UTF-8");
That's the code for the web view above.
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h2 style="color:red;">Hello Chief</h2><p>This is just a demo HTML content rendered beautifully</p><br><img src="file:///android_asset/folder1/folder2/placeholder.png"></body></html>
That's code above is for the data coming from the sqlite database.
My Assests folder looks something like this
-assets
- folder1
- folder2
-file
Thanks
There is one method in webview to load html file with base url
webview.loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)
In baseUrl give it as
file:///android_asset/
Or else parse the html document with Jsoup and replace the src of required tags using Jsoup Parser.

Showing HTML elements via WebView in android

I am trying to show some basic html elements in a webview. I can do it with load datawithbaseurl method. But is there any way to check if content loaded correctly ?
Here is my html code coming from webservice.
<div style="float:left;margin:0;padding:0;top:0;bottom:0"><img src="http://exam.exam.com/upload/images/Activities/poskbhov.jpg" alt="" /></div>
and my webview code :
webView.loadDataWithBaseURL("", content.Content, "text/html", "UTF-8", "");
I just want to show an image. But i want to make sure its loaded. Right now i dont have an image. WebView just shows an img object in a div right now. I would like to put this image can not shown alert.
To link an image, build an String (HTML format) and code an IMG element:
String htmlContent = "<HTML>.....<img id=\"main_image\" src=yourImage.png alt=\"\"></br>.....</HTML>");
Load the content telling the webView were your resources are, and put your IMG in your "assets" folder with the name "yourImage.png"
webView.loadDataWithBaseURL("file:///android_asset/", htmlContent, "text/html", "utf-8", null);
You can also Link css file from here too!
If you want to see a full example, take a look to this project:
https://github.com/matessoftwaresolutions/AndroidHttpRestService
It consists on an HTTPService and, in the class HttpServiceFragment you can see a full (specific) implementation.
I hope it helps!!!

How to add local css file to a page in WebView?

So, I have a WebView showing page from url:
WebView webView = (WebView) findViewById(R.id.showContentWebView);
String url = "http://edition.cnn.com/2014/02/05/sport/shaun-white-sochi-slopestyle/index.html";
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
And I also have a file "style.css" in my assets folder. How can I display the page in WebView with style.css connected?
I think this is possible only if you somehow process the HMTL content, adding a link to your css file. In this case, please refer to this question.

Loading package html + resources into webview

I would like to know how I can display a HTML page in webview with references to relative/local images. The goal is to have the html page and all linked images contained in the android application package itself.
Where would I need to place the assets (assets directory?) and how do I reference these so they load into the webview?
Thanx!
Sven
You can put all html and images to assets directory, like:
assets\html\
index.html
image1.png
image2.jpg
All references to the images in the html file should be in form of file:// url
<html>
<body>
<img src="file:///android_asset/html/image1.png">
<img src="file:///android_asset/html/image2.jpg">
</body>
</html>
After all, just load this HTML to WebView as usual:
webView.loadUrl("file:///android_asset/html/index.html");

Categories

Resources