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

五迷三道 提交于 2019-12-03 10:08:07

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.

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