java - 如何在原生 Android 应用程序中渲染 epub 文件?

标签 java android epub epublib

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

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

如何在 Android 中使用 epub 文件实现类似的效果? 我正在使用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<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);
    }
  }
}

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

谢谢。

最佳答案

可以向PageTurner Reader学习和 epub3reader

作为一种简单的方法,您可以将 WebView 与 nl.siegmann.epublib.browsersupport 包中的 Navigator 和 NavigatorEventListener 一起使用。尽管 WebView 不是“原生”的。

步骤如下:

  1. 在您的类中实现 NavigatorEventListener。
  2. 像下面的代码 fragment 一样初始化导航器:

    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. 完成。

关于java - 如何在原生 Android 应用程序中渲染 epub 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38053277/

相关文章:

jupyter-notebook - pandoc 的 --webtex 的独立替代品

CSS 图像大小调整

java - Android - 奇怪的 EscapeUtil.unescapeString 崩溃

java - 十六进制字符串到 int 的转换无法正常工作

java - 将图片放入word文档中

java - 使用 sqlite 更新数据库时没有错误,但没有更新(android 应用程序)

android - Android 8 或 9 的后台地理围栏不起作用

java - 格式化十进制数

java - 在 .xml 文件设计未显示在整个项目中

html - 文字在某些电子阅读器应用程序上不合理,但在其他应用程序上会