如何在本机Android应用程序中呈现epub文件?

我正在尝试制作一个能够渲染.epub文件并在Android布局中显示其内容的Android应用程序。

我做了一个类似的应用程序,使用pdfRenderer显示pdf,使用位图显示pdf页面。

如何在android中使用epub文件实现类似的function? 我正在使用Epublib,但我无法找到任何好的教程。 我也尝试过skyepub,但它似乎已经过时了。

任何人都可以帮我一个epublib的例子这是我的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 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); } } } 

我是编程新手,所以感谢任何帮助或支持。

谢谢。

您可以从PageTurner Reader和epub3reader学习

对于一种简单的方法,您可以使用来自nl.siegmann.epublib.browsersupport包的WebView和Navigator以及NavigatorEventListener。 虽然WebView不是“原生”的。

这里的步骤:

  1. 在类中实现NavigatorEventListener。
  2. 像以下代码段一样初始化Navigator:

     private void init() { mNavigator = new Navigator(); mNavigator.addNavigationEventListener(this); mNavigator.gotoBook(book, this); // book is from your loaded InputStream book mNavigator.gotoFirstSpineSection(this); } 
  3. 在您实现的NavigationPerformed中添加如下:

     @Override public void navigationPerformed(NavigationEvent navigationEvent) { if (navigationEvent.isResourceChanged()) { int currentSpinePos = navigationEvent.getCurrentSpinePos(); displayPage(navigationEvent.getCurrentResource(), currentSpinePos);navigationEvent.getCurrentResource().toString()); } } 
  4. 添加displayPage方法以显示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); } } 
  5. 完。