Search code examples
androidepub

Erro reading an epub file in Android


I want to read epub file page by page in my Android app.

I have tried but am getting a nullPointer Exception for at this line:

Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());

Can anyone explain why I am getting this error or how to do read books page by page?

Here is my Activity code:

public class LogTestBookInfo extends Activity
 {
   @Override
   public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    AssetManager assetManager = getAssets();

    try {

      // find InputStream for book
      InputStream epubInputStream = assetManager.open("assets/books/wodehouse.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());

    }

  }


  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);

    }

  }

}

Here is my logcat information:

12-04 11:12:08.606: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.EpubReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubReader' instead.
12-04 11:12:08.635: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.domain.Resource' exceeds maximum length of 23 characters, using 'n*.s*.e*.d*.Resource' instead.
12-04 11:12:08.886: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.EpubProcessorSupport' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.EpubProces*' instead.
12-04 11:12:08.996: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.PackageDocumentReader' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.PackageDoc*' instead.
12-04 11:12:09.336: I/AndroidLoggerFactory(334): Logger name 'nl.siegmann.epublib.epub.NCXDocument' exceeds maximum length of 23 characters, using 'n*.s*.e*.e*.NCXDocument' instead.
12-04 11:12:09.565: I/epublib(334): author(s): [B., Gummere, Francis]
12-04 11:12:09.565: I/epublib(334): title: Beowulf
12-04 11:12:09.606: E/AndroidRuntime(334): FATAL EXCEPTION: main
12-04 11:12:09.606: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bookapp/com.bookapp.LogTestBookInfo}: java.lang.NullPointerException
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 11:12:09.606: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:12:09.606: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
12-04 11:12:09.606: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 11:12:09.606: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 11:12:09.606: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
12-04 11:12:09.606: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
12-04 11:12:09.606: E/AndroidRuntime(334):  at com.bookapp.LogTestBookInfo.onCreate(LogTestBookInfo.java:80)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 11:12:09.606: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 11:12:09.606: E/AndroidRuntime(334):  ... 11 more

Solution

  • Change the code like this

     if(book.getCoverImage()!=null&&book.getCoverImage().getInputStream()!=null)  
     Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());