11

In my project I have a files:

"MyProject/assets/folder1/image1.jpg"
"MyProject/assets/folder1/index.html".

In webView I need to open index.html (with images).

I trying this code:

String baseUrl = "file:///android_asset/folder1/";
webView.loadDataWithBaseURL(baseUrl, readFileAsString("index.html") , mimeType, "UTF-8", null);

But images don't loading.

If I put images to "assets" directory (MyProject/assets/) and make baseUrl = "file:///android_asset" images are loaded correctly;

How load images not only from root assets directory, but and from assets/folder1?

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
user1367713
  • 286
  • 2
  • 4
  • 11
  • I saw a long post about this subject a while ago. If you still need some help with it look it up. http://devblog.morethanheroic.com/2017/01/03/how-to-create-an-app-from-a-static-website/ – Lakatos Gyula Jan 03 '17 at 18:05

4 Answers4

16

try like this

WebView webview = (WebView)this.findViewById(R.id.webview);


String html = "<html><head><title>TITLE!!!</title></head>";
html += "<body><h1>Image?</h1><img src=\"icon.png\" /></body></html>";


webview.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html", "UTF-8", null); 

For more information try this link

perfect LoadDataWithBaseurl

Boris
  • 316
  • 2
  • 10
Janmejoy
  • 2,721
  • 1
  • 20
  • 38
  • have heard that `baseURL` of this function will ignore anything that comes after first /. – Darpan Sep 04 '14 at 11:41
  • The baseURL lets you to run all the files such as .js/.css/.html files from their folders. For example, there is a folder and all .js/.css/.html files in it, and you call .html files where you locate the folder address as called baseURL: webview.loadDataWithBaseURL("file:///android_asset/myWebFilesInThisFolder/", "

    hello world

    " So, you don't have to write a full address for JS/CSS/HTML just bundle it!
    – Bay Sep 17 '19 at 11:21
  • @Janmejoy FYI the second link (`LoadDataWithBaseurl`) is pointing to `http://lomza.people.totem-soft.com/?p=62` which seems to be an invalid location. – Marino Aug 06 '21 at 13:23
5

I think you have to set the base to assets and add the sub folders to your image src's like this:

webView.loadDataWithBaseURL("file:///android_asset/", readAssetFileAsString("folder1/index.html"), "text/html", "UTF-8", null);

Html: <img src="folder1/image1.jpg">

This worked for me on Android 5.1

private String readAssetFileAsString(String sourceHtmlLocation)
{
    InputStream is;
    try
    {
        is = getContext().getAssets().open(sourceHtmlLocation);
        int size = is.available();

        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();

        return new String(buffer, "UTF-8");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

    return "";
}
behelit
  • 1,765
  • 2
  • 20
  • 34
  • 1
    added method code, this code was previously taken from another SO post but unfortunately I don't have the link to credit them – behelit Sep 19 '16 at 00:43
1

try to like this

try {
            String filePath = null;
            filePath = "Your File path";
            Bitmap bitmap = null;

            bitmap = BitmapFactory.decodeFile(filePath);
            Log.v("Image data-->", "" + bitmap);
            imageWidth = bitmap.getWidth();
            imageHeight = bitmap.getHeight();
            Log.e("Width", "" + imageWidth);
            filePath = "file://" + filePath;
            String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html\";charset=utf-8\"/><title></title></head><body style=\"width:"
                    + imageWidth
                    + "px; height:"
                    + imageHeight
                    + "px; background:url("
                    + filePath
                    + ") no-repeat; position:relative;\">"
                    + getDivTag(mapList)
                    + "</body></html>";

            Log.v("MIS", "" + html);
            webview.getSettings().setSupportZoom(true);
            webview.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);

            System.out.println(html);

        } catch (Exception e) {
            e.printStackTrace();
        }
Yogesh Tatwal
  • 2,722
  • 20
  • 43
-15

Have you give internet permission?

<uses-permission android:name="android.permission.INTERNET" />
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
  • Seriously? if He says putting them in one folder works well means he must have put the permission. – Darpan Sep 04 '14 at 10:57
  • @Darpan The answer is totally irrelevant so is your comment. This works because OP's assets are local and don't need the internet permission. Simply, "he must have put the permission." is invalid argument – Farid Jun 24 '21 at 13:25