Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top