如何在本机Android应用程序中呈现epub文件? [英] How to render a epub file in a native android app?

查看:229
本文介绍了如何在本机Android应用程序中呈现epub文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a android app that would render a .epub file and display its content in an android layout.

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

I did a similar app for displaying pdf's using pdfRenderer which displayed the pdf pages using bitmaps.

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

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.

任何人都可以帮我找一个epublib的例子这是我的epublib代码:

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.

谢谢。

推荐答案

您可以从 PageTurner Reader epub3reader

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

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.

这里的步骤:


  1. 在你的类中实现NavigatorEventListener。

  2. 初始化Navigator,如下面的代码片段:

  1. Implement the NavigatorEventListener in your class.
  2. 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);
}


  • 在您实施的NavigationPerformed中添加如下:

  • 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());
      }
    }
    


  • 添加displayPage方法以显示epub:

  • 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.

    这篇关于如何在本机Android应用程序中呈现epub文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆