Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load local image in Android WebView

I'm trying to load a html string stored in the database which contain a image into a WebView. The image is stored in the internal memory. I am giving a reference to the html string.But it doesn't work. Any help?

String content="<p>Can we have a rotational" +
    " symmetry of order more than 1 whose angle of rotation is&nbsp;</p>\n" +
    "\n <p>(i)&nbsp;<img alt=\"45\\degree\" src=\"file:///storage/emulated/01484890695248.jpg\" /></p>\n" +
    "\n<p>(ii)&nbsp;<img alt=\"35\\degree\" src=\"file:///storage/emulated/01484890697301.jpg\" /></p>";

WebView00.loadDataWithBaseURL("", content, "text/html","UTF-8", "");
WebView00.getSettings();
like image 766
Bera Avatar asked Feb 16 '17 06:02

Bera


2 Answers

Try like this

String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/image_name.jpg";
String html = "<html><head></head><body> <img src=\""+ imagePath + "\"> </body></html>";
webView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
like image 82
Charuක Avatar answered Nov 16 '22 02:11

Charuක


Put the images in your assets folder. Then use the corresponding prefix

file:///android_asset/
like image 43
Diego Torres Milano Avatar answered Nov 16 '22 03:11

Diego Torres Milano