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

前端 未结 1 900
面向向阳花
面向向阳花 2021-02-11 09:44

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 pdfRendere

相关标签:
1条回答
  • 2021-02-11 10:26

    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.

    0 讨论(0)
提交回复
热议问题