Android - local image in webview

AndroidImageWebviewAndroid Webview

Android Problem Overview


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 ?

Android Solutions


Solution 1 - Android

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");  

Solution 2 - Android

You can also try

String data = "<body>" + "<img src=\"large_image.png\"/></body>";
 
webView.loadDataWithBaseURL("file:///android_asset/",data , "text/html", "utf-8",null);

Solution 3 - Android

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.

  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.

Solution 4 - Android

Use this method.

mview.loadDataWithBaseURL(folder.getAbsolutePath(), content, "text/html", "windows-1252", "");

folder.getAbsolutePath() can be "file:///android_asset" or just "/"

Solution 5 - Android

I think there is a \ missing in your code

   String data = "<body>" + "<img src=\\"file:///android_asset/large_image.png\"/></body>";      

Solution 6 - Android

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' />

Solution 7 - Android

enter image description here

 webView.loadDataWithBaseURL("file:///android_asset/", sourse, "text/html", "UTF-8", null);

Solution 8 - Android

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");

Solution 9 - Android

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);

..

Solution 10 - Android

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.

Solution 11 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSt&#233;phane PietteView Question on Stackoverflow
Solution 1 - AndroidSanjay PatelView Answer on Stackoverflow
Solution 2 - AndroidZain AliView Answer on Stackoverflow
Solution 3 - AndroidErkki Nokso-KoivistoView Answer on Stackoverflow
Solution 4 - AndroidViren SavaliyaView Answer on Stackoverflow
Solution 5 - AndroidTanmay MandalView Answer on Stackoverflow
Solution 6 - AndroidCurtisView Answer on Stackoverflow
Solution 7 - AndroidGeorgy GobozovView Answer on Stackoverflow
Solution 8 - AndroidA SView Answer on Stackoverflow
Solution 9 - AndroidzeeawanView Answer on Stackoverflow
Solution 10 - AndroidAlfred BambergerView Answer on Stackoverflow
Solution 11 - AndroidGaurang GodaView Answer on Stackoverflow