I am trying to make a android app that would render a .epub file and display its content in an android layout.
I did a similar app for displaying pdf's using pdfRenderer which displayed the pdf pages using bitmaps.
How can I achieve something similar using a epub file in android? I am using Epublib but I am unable to find any good tutorials for it. I also tried skyepub but it seems quite outdated.
Can anyone help me with a example for epublib this is my code for epublib:
package org.inevitablesol.com.epubdemo;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_openepub = (Button) findViewById(R.id.btn_openepub);
btn_openepub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenEpubfile();
}
});
}
private void OpenEpubfile() {
AssetManager assetManager = getAssets();
try {
// find InputStream for book
InputStream epubInputStream = assetManager.open("books/The-Problems-of-Philosophy-LewisTheme.epub");
// Load Book from inputStream
Book book = (new EpubReader()).readEpub(epubInputStream);
// Log the book's authors
Log.i("epublib", "author(s): " + book.getMetadata().getAuthors());
// Log the book's title
Log.i("epublib", "title: " + book.getTitle());
// Log the book's coverimage property
Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
Log.i("epublib", "Coverimage is " + coverImage.getWidth() + " by "
+ coverImage.getHeight() + " pixels");
// Log the tale of contents
logTableOfContents(book.getTableOfContents().getTocReferences(), 0);
} catch (IOException e) {
Log.e("epublib", e.getMessage());
}
}
/**
* Recursively Log the Table of Contents
*
* @param tocReferences
* @param depth
*/
private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
if (tocReferences == null) {
return;
}
for (TOCReference tocReference : tocReferences) {
StringBuilder tocString = new StringBuilder();
for (int i = 0; i < depth; i++) {
tocString.append("\t");
}
tocString.append(tocReference.getTitle());
Log.i("epublib", tocString.toString());
logTableOfContents(tocReference.getChildren(), depth + 1);
}
}
}
I am new to programming so any help or support is appreciated.
Thank you.
You can learn from PageTurner Reader and epub3reader
For a simple way you can use WebView with Navigator and NavigatorEventListener from nl.siegmann.epublib.browsersupport package. Though WebView is not a 'native' one.
Here the steps:
- Implement the NavigatorEventListener in your class.
Initialize Navigator like the following snippet:
private void init() { mNavigator = new Navigator(); mNavigator.addNavigationEventListener(this); mNavigator.gotoBook(book, this); // book is from your loaded InputStream book mNavigator.gotoFirstSpineSection(this); }
In your implemented NavigationPerformed add like this:
@Override public void navigationPerformed(NavigationEvent navigationEvent) { if (navigationEvent.isResourceChanged()) { int currentSpinePos = navigationEvent.getCurrentSpinePos(); displayPage(navigationEvent.getCurrentResource(), currentSpinePos);navigationEvent.getCurrentResource().toString()); } }
Add displayPage method to show epub:
private void displayPage(Resource resource, int sectionPos) { if (resource == null) { return; } try { mWebView.loadDataWithBaseURL("file:///android_asset/data/", data, "text/html", "UTF-8", null); } catch (Exception e) { Log.d(TAG, "When reading resource " + resource.getId() + "(" + resource.getHref() + ") :" + e.getMessage(), e); } }
Finish.
来源:https://stackoverflow.com/questions/38053277/how-to-render-a-epub-file-in-a-native-android-app