Skip to content Skip to sidebar Skip to footer

How To Open .epub Files In Android

I'm doing an android application which needs to read .EPUB files.I'm using http://www.siegmann.nl/epublib/android epublib for achieving this. But I got below error: 01-09 12:52:09.

Solution 1:

Based on your comments, error is in this line:

BitmapcoverImage= BitmapFactory.decodeStream(book.getCoverImage().getInputStream());

What is being thrown is NullPointerException, which means that:

  1. book is null
  2. book.getCoverImage() returns null
  3. book.getCoverImage().getInputStream() returns null

You have to check which one of those is happening (use Log.d() and simply print out those values, or use debugger).

Possible reasons I can think of:

  1. book could be null on example because "sample.epub" is missing, or it's format is invalid and EpubReader fails in decoding it.
  2. Book does not have cover image?

Solution 2:

Raeder part load epub file like this from assets folder

AssetManagerassetManager= getAssets();
        try {
            InputStreamepubInputStream= assetManager.open("books/hafez.epub");
            Bookbook= (newEpubReader()).readEpub(epubInputStream);
            logContentsTable(book.getTableOfContents().getTocReferences(), 0);
        } catch (IOException e) {
            Log.e("epublib", e.getMessage());
        }

load tableOfContent of epub

privatevoidlogContentsTable(List<TOCReference> tocReferences, int depth) {
    if (tocReferences == null) {
        return;
    }
    for (TOCReferencetocReference:tocReferences) {
        StringBuilder tocString = newStringBuilder();
        for (int i = 0; i < depth; i++) {
            tocString.append("\t");
        }
        tocString.append(tocReference.getTitle());
        RowData row = newRowData();
        row.setTitle(tocString.toString());
        row.setResource(tocReference.getResource());
        contentDetails.add(row);
        logContentsTable(tocReference.getChildren(), depth + 1);
    }
}

privateclassRowData{
    privateString title;
    privateResource resource;

    publicRowData() {
        super();
    }

    publicStringgetTitle() {
        return title;
    }

    publicResourcegetResource() {
        return resource;
    }

    publicvoidsetTitle(String title) {
        this.title = title;
    }

    publicvoidsetResource(Resource resource) {
        this.resource = resource;
    }

}

show a chapter of epub in WebView for example

WebViewwebView= (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    WebSettingssettings= webView.getSettings();
    settings.setDefaultTextEncodingName("utf-8");

    StringdisplayString= rowData.getResource().getData();
    if (displayString != null)
        webView.loadDataWithBaseURL(null, displayString, "text/html", "UTF-8", null);

Post a Comment for "How To Open .epub Files In Android"