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!!!
Related
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);
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");
I know that for security reasons HTML standard doesn't allow anymore the loading of a local resource as image in a document.
Anyway I found that I have to copy the html files in the android-asset project folder if I want to load local pages in a WebView.
What if I want to use an hybrid approach?
I would to get a HTML document from an uri, replace the value of the src attribute of a img HTML tag with a local path and then load the code in a WebView.
Having a html document like this:
<hmtl>
<head>
</head>
<body>
<img src="http://URL-SITE/img/x.jpg">
</body>
</hmtl>
How can i replace http://URL-SITE/img with the path of a local image resource
before the page is loaded by a WebView?
I already tried putting the local image in the folder android-asset and changing the src value in file:///android_asset/img/x.jpg but it didn't work.
Ok guys, thanks for the comments.
The problem was that I used the command loadData
String html ="<html> <head></head> "
+" <body> <img src=\"file:///android_asset/img/x.jpg\"> "
+"</body> </html>";
myWebView.loadData(html, "text/html; charset=UTF-8", null);
instead of
myWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
For my android app I want to load a html file to webview, this html file will be stored in "data/data/com.myapp/files/index.html". I am able to load it with the associated javascript files but when I want to load the image from "data/data/com.myapp/files/img/image.png" it doesn't work.
This is how i'm trying to do it:
webView.loadDataWithBaseURL(getFilesDir()+"/img/", html, "text/html","utf-8", ""); //html is my html string I get from index.html
I've also tried:
webView.loadUrl(getFilesDir()+"/img/"+index.html");
In my html file i have
<img id="image" src="/img/image.png"/>
I've verified and the file is loaded on the disk but i can't display it on webview, the display stays blank for image. I just can't make it work. So my question is: is it even possible? If yes can someone please tell me how?
I've searched and found that others had similar problems but none of the solutions is working.
EDIT: Putting my files in asset directorty is not an option for me, which i've already tested and it works.
I just tried the following code I found here: Load the image saved in sdcard in webview
String base = getFilesDir();
String imagePath = base + "/test.jpg";
String html = ("<html>
<head>
</head>
<body>
<img src=\""+ imagePath + "\">
</body>
</html>
");
mWebView.loadData(html, "text/html","utf-8");
But still no success, apparently it worked for the person who asked the question. I've also tested with the external storage but the result is same. All permissions are there in manifest.xml. So I really don't see what the problem is. Any suggestion or advice will be appreciated.
Consider putting static files in assets folder inside android project, and try loading file using:
mWebView.loadUrl("file:///android_asset/html/index.html");
And then, from HTML file refer to images, styles using relative path.
For example, in your case, you would have follwing directory structure:
PROJECT-ROOT
|-src/
|-assets/
|-html/index.html
|-img/image.png
|-css/style.css
After this, you can use normal html tags inside index.html as you would do to make any web page. Eg. <img id="image" src="img/image.png"/> or <link rel="stylesheet" type="text/css" href="css/style.css">
Don't forget to enable JavaScript using:
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Problem solved. It was actually coming from bad formatted data, once I checked and added correct files to phone memory it started working.
When you want to save image to internal storage, be sure not to use same code as downloading html file. That's the mistake I made. Instead, use
How to download and save an image in Android
to save your image, then the image should be fine
I'm trying to diplay a local image in my webview :
String data = "<body>" + "<img src=\"file:///android_asset/large_image.png\"/></body>";
webview.loadData(data, "text/html", "UTF-8");
This code doesn't display anything, instead of :
webview.loadUrl("file:///android_asset/large_image.jpg");
This one works, but I need to have complex web page, not just a picture.
Any ideas ?
Load Html file in Webview and put your image in asset folder and read that image file using Html.
<html>
<table>
<tr>
<td>
<img src="abc.gif" width="50px" alt="Hello">
</td>
</tr>
</table>
</html>
Now Load that Html file in Webview
webview.loadUrl("file:///android_asset/abc.html");
You can also try
String data = "<body>" + "<img src=\"large_image.png\"/></body>";
webView.loadDataWithBaseURL("file:///android_asset/",data , "text/html", "utf-8",null);
One convenient way (often forgotten) is to use base64 embedded images in HTML content. This will also work on mobile Webkit browsers (IOS, Android..).
Point of using this method is that you can embed images on HTML content, instead of fighting with image links from webview to restricted filesystem.
<img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
xxxxx = base64 encoded string of images bytes
If you want to provide (base64 embedded) image data from filesystem, you can for example:
1) In Android use ContentProvider - which will provide base64 formatted image strings.
<img src="content://.............."/>
2) Or you can preprocess HTML with JSOUP or similar DOM parser (before setting it to webview) and adjust image src with properly base64 encoded image.
Disadvantages of this method is overhead included in converting image to base64 string and of course in proding larger HTML data to webview.
Use this method.
mview.loadDataWithBaseURL(folder.getAbsolutePath(), content, "text/html", "windows-1252", "");
folder.getAbsolutePath() can be "file:///android_asset" or just "/"
I think there is a \ missing in your code
String data = "<body>" + "<img src=\\"file:///android_asset/large_image.png\"/></body>";
The image will not load unless you have:
webSettings.setAllowFileAccessFromFileURLs(true);
If you are using a html file, it should be in a folder called "assets" in /app/src/main. If you don't have that folder then make it. Then load the html file with:
myWebView.loadUrl("file:///android_asset/test.html");
If you put the image in the same folder as the html file, then in the html file you can just do a normal:
<img src='myimage.jpg' />
webView.loadDataWithBaseURL("file:///android_asset/", sourse, "text/html", "UTF-8", null);
the best approach for me was to create my .html file with all the texts and images in MS word, and save the file as an .html file and copying both .html file and the corresponding attachments folder into assets folder and giving the address of .html file in asset folder to webview.loadUrl()...
Here is what you need to Do...
WebView webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/learning1/learning1.htm");
Zain's solution worked for me. I forgot to add my folder www having HTML files and other subfolders of css and images etc.
webView.loadDataWithBaseURL("file:///android_asset/www/",data , "text/html", "utf-8",null);
..
The most simple and straightforward way is to create a html file with a html editor like kompozer or whatever you like.
Put your html file in the assets folder and call webView.loadUrl(filename). The assets folder should also contain all pictures which you are referencing in your html files.
Correct in advance in the html file the path to your images in a way, that you only file down the pure filename. The file name you pass to loadUrl must be prefixed with file:///android_asset/.
If the picture or file does not load, check the filenames for blanks, hyphen and other weird stuff and change the filenames.
I Know the Question is answered correctly, but I am going to implement in an effective way.
Step-1 make an HTML file in asset folder
ex:-imageLaod.html
Note: Here In HTML file we have implemented FullScreen Image by giving style="width:100%"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Load Image</title>
<style type="text/css">
h3{
color:blue;
}
</style>
</head>
<body>
<h3>This image loaded from app asset folder.</h3>
<img src="yourimage.png" style="width:100%" alt="companylogo"/>
</body>
</html>
Step-2 put your image in the asset folder. (ex:yourimage.png)
Step-3 put below code in java file.
String folderPath = "file:android_asset/";
String fileName = "loadImage.html";
String file = folderPath + fileName;
WebView webView = findViewById(R.id.webview)
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.loadUrl(file);
and it is done. you can see the fullscreen image with zoom in-zoom out feature of WebView.
Hope it helps.