Mueyiwa Moses Ikomi
Mueyiwa Moses Ikomi

Reputation: 1142

android - Loading HTML from database into WebView

so i'm trying to load some HTML from my sqlite database into a webview. This works splendid as the html is rendered beautifully but here comes the problem. The local file added in the html is not being load. I added it to the assets folder in this hierarchy: assets/folder1/folder2/placeholder.png but i get a Not allowed to load local resource: error. How can i get past this please.

Please note that the data is being populated from the db.

webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.loadData(data, "text/html", "UTF-8");

That's the code for the web view above.

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h2 style="color:red;">Hello  Chief</h2><p>This is just a demo HTML content rendered beautifully</p><br><img src="file:///android_asset/folder1/folder2/placeholder.png"></body></html>

That's code above is for the data coming from the sqlite database.

My Assests folder looks something like this

-assets
  - folder1
    - folder2
      -file

Thanks

Upvotes: 1

Views: 1334

Answers (1)

Sangeet Suresh
Sangeet Suresh

Reputation: 2545

There is one method in webview to load html file with base url

webview.loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)

In baseUrl give it as

file:///android_asset/

Or else parse the html document with Jsoup and replace the src of required tags using Jsoup Parser.

Upvotes: 1

Related Questions