0
votes

I am trying to Load an html file from Device's Internal Storage,

I've Created a separate (folder/directory) for html files then added an Image and .html file in it.

Here, my WebView is showing all other tags content expect image tag's src.

Please help me load the image from file from internal storage to WebView

Here is my code.

 File dir = new File(android.os.Environment.getExternalStorageDirectory()+"/"+"HTML Folder"+"/"+"index.html");
    if(dir.exists()) {
        File file = new File(String.valueOf(dir));
        StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
            br.close();
        } catch (IOException e) {
        }

        TextView i = findViewById(R.id.codeviewText);
        i.setText(text);

        String code = i.getText().toString();
        mWeb.loadUrl("data:text/html,".concat(code));
        mWeb.setWebChromeClient(new WebChromeClient());
        mWeb.getSettings().setJavaScriptEnabled(true);

and Below is Screenshots

screenshot

1
please add index.html content. to your question - R7G
I re-writted the html file ,here is my index.html file : drive.google.com/file/d/1qvDxEG-3tgBeSljp-d_fFUHkYdWXO-rf/… - Shaik
Only post the code for an img tag here. - blackapps
<img src="banner.jpg" alt="banner"> - Shaik
Only if you used an absolute path for your image your strange construction would work. As Emil suggested load the file directly then you can use a relative path as you have now. Or use baseurl if you have a lot of img tags. - blackapps

1 Answers

0
votes

You should just load the html file URI like this:

mWeb.loadUrl("file:///path/to/your_page.html")

To obtain your html file URI, use

Uri.fromFile(file)