android下解析epub格式书籍的相关资料不多,查了很久找出了一些相关资料,找到了一个epublib包,
Epublib开发android的主页:http://www.siegmann.nl/epublib/android
相关jar包下载:https://github.com/downloads/psiegman/epublib/epublib-core-latest.jar
http://www.slf4j.org/android/slf4j-android-1.6.1-RC1.jar
在线API:http://www.siegmann.nl/static/epublib/apidocs/
具体的书籍信息提取代码如下:
package com.zyj.ui;
import java.io.IOException;
import java.io.InputStream;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Metadata;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.epub.EpubReader;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private TextView bookInfoText;
private ImageView coverImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bookInfoText = (TextView) findViewById(R.id.bookInfoText);
coverImage = (ImageView) findViewById(R.id.coverImage);
try {
InputStream is = getAssets().open("timefriend.epub");
Book book = new EpubReader().readEpub(is);
Metadata metadata = book.getMetadata();
String bookInfo = "作者:"+metadata.getAuthors()+
"\n出版社:"+metadata.getPublishers()+
"\n出版时间:" +metadata.getDates()+
"\n书名:"+metadata.getTitles()+
"\n简介:"+metadata.getDescriptions()+
"\n语言:"+metadata.getLanguage()+
"\n\n封面图:";
bookInfoText.setText(bookInfo);
nl.siegmann.epublib.domain.Resources ress = book.getResources();
Resource res = ress.getById("cover");
byte[] data = res.getData();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
coverImage.setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
说明:epub格式的书籍其实就一个zip压缩包,可以将其后缀名改为.zip然后解压缩后查看文件目录。
遇到的问题:
在获取书籍封面图片的时候通过epub主页提供的方法
Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage()
.getInputStream());
解决办法:通过book.getResources()获得Resources对象,然后通过方法getById("cover")获得Resource对象,这样就可以取到封面图信息了。
程序运行截图:
原文地址:http://blog.csdn.net/iamlixiaolong/article/details/7484436