Is it possible to display an image using the loadDataWithBaseURL () method in android?

I am trying to display the contents of an html file using the loadDataWithBaseURL () method in android.

I have a string containing the data of an Html file in a single source called String, and then pass this to the method.

eg,

String source; //contain html tags with images View.loadDataWithBaseURl(null,source,"text/html","UTF-8","about:blank"); 

The data displayed in the field of view is beautiful. My problem is that if there are any images in my html file, then I could not display it? How can i do this?

+6
android html image webview
source share
4 answers

you can do this if the images in the source use relative locations for src, then you need to set baseUrl to the "base" where the images will be located. for example, if you downloaded the google homepage from a source, it would look like this:

 View.loadDataWithBaseURI("http://google.com",source,"text/html","UTF-8","about:blank"); 

This indicates the web browser where images will be downloaded.

As a side note, I don’t think that URIs β€œfile: //” work in a web view for security reasons.

+5
source share

use " file: /// android_res / raw / " as the base URL and put your files in res / raw in your project.

RES / raw / index.html

Res / raw / image.jpg

 InputStream htmlStream = getResources().openRawResource(R.raw.index); Reader is = new BufferedReader(new InputStreamReader(htmlStream, "UTF8")); // read string from reader String html = readFile(is); webView.loadDataWithBaseURL("file:///android_res/raw/", html, "text/html", "UTF-8", null); 
+3
source share

I made a tutorial on how to use the loadDataWithBaseURL () method to display images - http://lomza.totem-soft.com/tutorial-using-webview-to-load-the-html-page-from-assets/#header

+2
source share

For example, if you want to use some images from an SD card, then your code should look something like this:

final String path = Environment.getExternalStorageDirectory() + File.separator + "YourFolderName"); bookView.loadDataWithBaseURL("file://" + path, htmlTextWithHeadAndBody, "text/html", "UTF-8", "");

+1
source share

All Articles