1

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

3
  • Please give a look on stackoverflow.com/questions/3033902/… Commented Aug 19, 2016 at 10:43
  • how did you achieve that? (loading the html from sqlite to a web view). Could you share some code please? Commented Jul 3, 2017 at 4:38
  • oh wow, that's like almost a year ago @Mwas, all of the code is lost somewhere. But is just saving your html into the sqlite db and outputting it. Nothing out of the ordinary really Commented Jul 3, 2017 at 6:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, but now i get a Unable to open asset URL: file:///android_asset/folder1/folder2/placeholder.png. Do i remove the (File) in my <img src="file:///android_asset/folder1/folder2/placeholder.png"> since its the base url now and link to it relatively? like <img src="/folder1/folder2/placeholder.png">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.