2

I found out that in earlier APIs of android (KitKat and abover), local images (for example pictures in assest folder) can't be load in WebView! I have a html file that contains tag to show images.

<img src="blacksmoke1.jpg">

And I put blacksmoke1.jpg in assest folder. but nothing shown in WebView.

This is the problem: https://code.google.com/p/android/issues/detail?id=63033

How can I fix it? Is there an alternative way to show pictures in webview? Or Is there a custom WebView that i can implement in my app?

Edit: This my assest folder:

enter image description here

Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
  • Please read question completely! I know how to display a html file in WebView... :| The problem is in KitKat WebView... It can't access to local files in app such as images... I looking for a solution for this problem! – Saman Salehi Dec 29 '14 at 16:42

2 Answers2

3

Try creating a web pages in your assets directory and creating HTML pages that display the images. Then call a web page using this:

webview.loadUrl("file:///android_asset/blacksmoke.html");

Or, according to one of the posts in your link, move the html file to a server and use this code:

loadDataWithBaseUrl("content://<your contentProvider>/blacksmoke.html", ...);

Code in blacksmoke.html could be:

<!DOCTYPE html>
<!-- Perhaps some JQuery mobile for more functionality and control over which images get displayed and why etc -->
  <body>
     <div><img src="image/blacksmoke1.jpg"></div>
  </body>
</html>

Here is a good explanation for creating native pages or displaying native images.

Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
  • It's not display local images in earlier APIs... – Saman Salehi Dec 29 '14 at 15:47
  • @K2evil Then do not try to display the image directly through Android, try using the html page to render the image leveraging webView. – Mr. Concolato Dec 29 '14 at 17:51
  • I coudn't understand how to move html file to a server! Can you give me a example or instruction ? – Saman Salehi Dec 30 '14 at 13:16
  • @K2evil Do you have a web server? Do you have a website or a web hosting account? If so, then you can place the file there. If not, then you can either get one for cheap with say Host Gator: http://www.hostgator.com/ or the abandon the server idea. – Mr. Concolato Dec 30 '14 at 14:51
0

This is what I do to load local resources in webview from assets folder:

    StringBuilder sb = new StringBuilder();
    if (notification.get_id().equals("53be76c3d6eac5f54a546176")){
        sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <title>For....    <img src=\"data1/images/forum1.jpg\" alt=\"\" title=\"\" id=\"wows1_0\"/> </body> </html>");

That is the html i want to load, if you notice there is an img tag, loading an image from data1 folder which is placed in assets folder. In order to be able to load resources from there I do the following:

    WebView webView = (WebView) findViewById(R.id.webview);

    webView.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html", "utf-8", null);

Hope it helps

Álvaro Pérez Soria
  • 1,443
  • 1
  • 13
  • 26
  • I have a html file in my assest folder and I can't build it in runtime because it has a lot of texts... please see my edit... – Saman Salehi Dec 29 '14 at 16:00
  • That was just an example. You can load your html file like this: webview.loadUrl("file:///android_asset/blacksmoke.html") – Álvaro Pérez Soria Dec 29 '14 at 16:01
  • Ok... it's how to show a html file in webview! it's not my problem! Did you see this link? https://code.google.com/p/android/issues/detail?id=63033 – Saman Salehi Dec 29 '14 at 16:21