【问题标题】:Erro reading an epub file in Android在 Android 中读取 epub 文件时出错
【发布时间】:2014-12-04 05:52:55
【问题描述】:

我想在我的 Android 应用中逐页阅读 epub 文件。

我已经尝试过,但在这一行得到了一个 nullPointer 异常:

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

谁能解释我为什么会收到此错误或如何逐页阅读书籍?

这是我的活动代码:

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

    }

  }

}

这是我的 logcat 信息:

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

【问题讨论】:

  • 仅供参考。如果您正在开发 epub 阅读器,我建议您使用一些开源 epub 阅读器项目并对其进行修改,因为从头开始开发需要花费大量时间和精力。

标签: android epub


【解决方案1】:

这样改代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多