Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - using Jsoup with android_asset html file

Tags:

android

jsoup

Ok well I have been using Jsoup to parse html from remote urls using:

Jsoup.connect(url).timeout(20000).get();

I am now trying to read local html files which I have stored in the assets folder. I have done a lot of searching but I cannot find a solution. On the Jsoup example - Load a Document from a File, they say to do the following:

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

From what I've read, the path to my file would be - file:///android_asset/results_2009.html.

enter image description here

However I always get no such file or directory, so how do I get a local file into Jsoup?

Do I need to use AssetManager or something? Please can someone point me in the right direction.

like image 230
Neil Avatar asked Dec 28 '12 14:12

Neil


1 Answers

Jsoup.parse() has an overload which takes an InputStream. You can use the AssetManager to obtain an InputStream to your file and use it:

InputStream is=null;

try {
    is=getAssets().open("results_2009.html");
    Document doc = Jsoup.parse(is, "UTF-8", "http://example.com/");
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(is!=null)
        is.close();
}
like image 197
rciovati Avatar answered Sep 17 '22 06:09

rciovati