How to render a epub file in a native android app?

ぃ、小莉子 提交于 2019-12-04 14:39:10

问题


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.


回答1:


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:

  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);
    }
    
  3. 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());
      }
    }
    
  4. 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);
      }
    }
    
  5. Finish.



来源:https://stackoverflow.com/questions/38053277/how-to-render-a-epub-file-in-a-native-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!