Android - local image in webview
I am 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 does not display anything, not:
webview.loadUrl("file:///android_asset/large_image.jpg"); This works, but I need a complex web page, not just an image.
Any ideas?
Upload the Html file to Webview and put your image in the resources folder and read this image file using Html.
<html> <table> <tr> <td> <img src="abc.gif" width="50px" alt="Hello"> </td> </tr> </table> </html> Now upload this html file to 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 inline base64 images in HTML content. This will also work in Webkit mobile browsers (iOS, Android ..).
The point of using this method is that you can embed images in HTML content, rather than fighting links to images from a web view in a limited file system.
<img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/> xxxxx = base64 encoded string of images bytes If you want to provide (base 64) image data from a file system, you can, for example:
1) Android uses ContentProvider - which will provide strings with base64 formatting.
<img src="content://.............."/> 2) Or you can pre-process the HTML using JSOUP or a similar DOM parser (before installing it in the webview) and configure the src image with the corresponding base64 encoded code.
The disadvantages of this method are the overhead involved in converting the image to a base64 string and, of course, when processing large HTML data in a webview.
Use this method.
mview.loadDataWithBaseURL(folder.getAbsolutePath(), content, "text/html", "windows-1252", ""); folder.getAbsolutePath() can be "file:///android_asset" or just "/"
I think the code is missing \
String data = "<body>" + "<img src=\\"file:///android_asset/large_image.png\"/></body>"; 
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 the MS-word and save the file as a .html file and copy both the .html file and the corresponding folder of investments in the assets folder and specifying the address of the .html file in the folder with resources on 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 www folder with HTML files and other subfolders of css and images, etc.
webView.loadDataWithBaseURL("file:///android_asset/www/",data , "text/html", "utf-8",null); ..
The easiest and easiest way is to create an html file with an html editor such as kompozer or whatever you like.
Put your html file in the resources folder and call webView.loadUrl(filename) . The assets folder should also contain all the images that you specify in your html files.
Correct in advance in the html file the path to your images so that you only write a clean file name. The file name that you pass to loadUrl must be prefixed with file:///android_asset/ .
If the image or file does not load, check the file names for spaces , hyphens and other strange things and change the file names.