0

I've seen a lot of similar questions here, but none of them seem to be exactly my problem.

I'm loading a local html file from a subfolder of my assets folder into a WebView. The file is located in assets/myfolder/myfolder2/test.html.

Also in assets/myfolder/myfolder2/ I have image.jpg. Full path: assets/myfolder/myfolder2/image.jpg

I am loading the html like so:

// html contains the string content of the test.html file
webView.loadDataWithBaseURL("file:///android_asset/myfolder/myfolder2/", html,
                                   "text/html", "utf-8", "about:blank");

In the html string, I have the following tag:

<img src="image.jpg" />

The image is not displayed. I've tried spelling out the full file:/// url to the image as well, with no luck. If I change it to link to an image somewhere out on the web, it works fine. Why can't it find my local image?

mattgmg1990
  • 5,576
  • 4
  • 21
  • 26

2 Answers2

1

After much frustration, I finally found out that I couldn't view this particular image on any Android device, even from the stock browser, despite it being what I thought was a normal JPG image.

I'm still not sure what was wrong with it, but when I opened it up in my photo editor and saved it again with normal JPG encoding, it worked. That includes when I reference it from the assets folder in my WebView.

mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • have you found out finally what the problem was? – olidem Jan 14 '18 at 21:00
  • No, I don't think I ever did. – mattgmg1990 Jan 16 '18 at 15:58
  • Ok, I found my problem out: I copied a folder in finder and pasted it in Android Studio. Somehow, some of the pasted images had size 0 bytes! I don't know why, but it created about 50% of all image files as empty files (with correct name). – olidem Jan 19 '18 at 13:07
0

To load the html from asset folder you should use webview.loadUrl().

  • I have created two subfolders inside assets folder named "folder1" and "folder2".

  • Have Put an image in folder2.

  • Have Put an HTML file with image tag "<img src="ic_launcher.png">"

and at last used webView.loadUrl("file:///android_asset/folder1/folder2/test.html");

I can see the image loaded in webview.

Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29
  • Thanks so much for your help. Unfortunately, it seems that my problem was completely unrelated, though I didn't realize it at the time. The image must have had something different about it's encoding, so it wouldn't display on any android device, despite being found in the assets folder without a problem. – mattgmg1990 Jul 27 '13 at 15:52