user3783249
user3783249

Reputation: 11

Android webview in web/website page use local asset file (help)

I am unable to use local app asset(example script,image) file in website html page

example :(webview) open page http://website.com/sample.html

and this sample.html

`<html><head><title>Error Internet</title>
</head><body>
<img src="file:///android_asset/image.png">
                    <h2><strong>&copy; 2014 -2015</strong></h2></body></html>

`

Page open but local file not open file:///android_asset/image.png

Upvotes: 1

Views: 912

Answers (2)

live-love
live-love

Reputation: 52366

In Android Studio, create an assets folder if you don't have one.

File - New - Folder - Assets folder

Place your image in the assets folder.

Use this code to load a webview with the image from your assets folder:

WebView web_view = findViewById(R.id.web_view_tts);
String html = "<!DOCTYPE html>\n" +
        "<html>\n" +
        "    <head>\n" +
        "    </head>\n" +
        "    <body>\n" +
        "<img src=\"file:///android_asset/volume_up.png\">" +
        "    </body>\n" +
        "</html>";

WebSettings webSettings = web_view.getSettings();

web_view.setWebViewClient(new WebViewClient());
webSettings.setJavaScriptEnabled(true); //use these settings for javascript
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);

web_view.loadDataWithBaseURL("file:///android_asset", html, "text/html", "utf-8", null);

Upvotes: 0

Samoka
Samoka

Reputation: 1333

just remove file:///android_asset/ in the html, src="pathOfImage/image.png" will just do. if you're loading that html from an asset.

Upvotes: 1

Related Questions