0
我想在我的Android應用程序中逐頁閱讀epub文件。Erro閱讀Android中的epub文件
我都試過,但我在這行獲得一個空指針異常:
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閱讀器項目並對其進行修改,因爲從頭開發需要花費大量時間和精力。 – 2014-12-04 06:55:54