Search code examples
androidepub

How to display all pages and all chapters using nl.siegmann.epublib


This is what I tried to perform the task if anyone can help out it would be most appreciated. So in this code it will display just the cover page. I read http://www.siegmann.nl/static/epublib/apidocs/ that you could use getSpine() to get everything but it only displayed one thing on my case which is cover page.

webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
AssetManager am = getAssets();
try {
    InputStream epubInputStream = am.open(bookName);
    book = (new EpubReader()).readEpub(epubInputStream);
} catch (IOException e) {
    Log.e("epublib", e.getMessage());
}

Spine spine = book.getSpine(); 
for (SpineReference bookSection : spine.getSpineReferences()) {
    Resource res = bookSection.getResource();

    try {
        InputStream is = res.getInputStream();
        StringBuffer string = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        try {
            while ((line = reader.readLine()) != null) {
                linez =   string.append(line + "\n").toString();
            }
        } catch (IOException e) {e.printStackTrace();}

        //do something with stream
    } catch (IOException e) {
        e.printStackTrace();
    }

}
webView.loadData(linez, "text/html", "utf-8");

Solution

  • So what I figured out using spine on http://www.siegmann.nl/static/epublib/apidocs/ is that it still works by sections. So I tried to figure out how many sections are there by identifying count numbers. Then placed those numbers in Resource res = spine.getResource(i);. If you would do Resource res = spine.getResource(2); It would display the spine of 2 which should be chapter 2 unless someone messes up the format of the epub.

    Spine spine = book.getSpine(); 
    List<SpineReference> spineList = spine.getSpineReferences() ;
    int count = spineList.size();
    tv.setText(Integer.toString(count));
    StringBuilder string = new StringBuilder();
    for (int i = 0; count > i; i++) {
        Resource res = spine.getResource(i);
    
        try {
            InputStream is = res.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            try {
                while ((line = reader.readLine()) != null) {
                    linez =   string.append(line + "\n").toString();
                }
    
            } catch (IOException e) {e.printStackTrace();}
    
            //do something with stream
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    webView.loadData(linez, "text/html", "utf-8");