Java 如何在android中读取Epub文件的所有内容?
我想阅读所有toc的所有内容Java 如何在android中读取Epub文件的所有内容?,java,android,epub,Java,Android,Epub,我想阅读所有toc的所有内容你能告诉我如何阅读目录(toc)中的所有章节吗?请发布修改后的代码,这样我们就可以阅读所有内容,或者发布一些东西,这样我就可以阅读内容了 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AssetManager assetsmanger=
你能告诉我如何阅读目录(toc)中的所有章节吗?
请发布修改后的代码,这样我们就可以阅读所有内容,或者发布一些东西,这样我就可以阅读内容了
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager assetsmanger=getAssets();
try {
// find InputStream for book
InputStream epubInputStream=assetsmanger.open("books/INCOME-TAX-ACT-1961.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());
String data=new String (book.getContents().get(3).getData());
String k=data;
web1=(WebView)findViewById(R.id.webView1);
// txt1=(TextView) findViewById(R.id.textView1);
// txt1.setText(k);
web1.loadData(k," text/html", "utf8");
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);
// list=(ExpandableListView)findViewById(R.id.expandableListView1);
// img1=(ImageView)findViewById(R.id.imageView1);
// img1.setImageBitmap(coverImage);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("epublib", e.getMessage());
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
// TODO Auto-generated method stub
if(tocReferences== null ){
return;
}
for(TOCReference tocReference:tocReferences){
StringBuilder tocstring=new StringBuilder();
for(int i=0;i<depth;i++)
{
tocstring.append("\t");
}
HashMap<String, String> map = new HashMap<String, String>();
String k= tocstring.append(tocReference.getTitle()).toString();
map.put("TOC",k);
ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
list1.add(map);
String t=k;
Log.i("epublib", tocstring.toString());
logTableOfContents(tocReference.getChildren(), depth + 1);
}
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager AssetManager=getAssets();
试试{
//查找书籍的输入流
InputStream epubInputStream=资产管理人.open(“账簿/所得税法案-1961.epub”);
//从inputStream加载图书
BookBook=(新的EpubReader()).readEpub(epubInputStream);
//记录这本书的作者
Log.i(“epublib”,“作者:”+book.getMetadata().getAuthors());
//记录书名
Log.i(“epublib”,“title:+book.getTitle());
字符串数据=新字符串(book.getContents().get(3.getData());
字符串k=数据;
web1=(WebView)findviewbyd(R.id.webView1);
//txt1=(TextView)findViewById(R.id.textView1);
//txt1.setText(k);
web1.loadData(k,“text/html”,“utf8”);
位图coverImage=BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
Log.i(“epublib”,“Coverimage是”+Coverimage.getWidth()+“by”
+coverImage.getHeight()+“像素”);
//记录内容的故事
内容日志表(book.getTableOfContents().gettoReferences(),0);
//列表=(ExpandableListView)findViewById(R.id.expandableListView1);
//img1=(ImageView)findViewById(R.id.imageView1);
//img1.setImageBitmap(封面图像);
}捕获(IOE异常){
//TODO自动生成的捕捉块
Log.e(“epublib”,e.getMessage());
}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
内容的私有void日志表(列表引用,int深度){
//TODO自动生成的方法存根
if(tocpreferences==null){
返回;
}
for(TOCReference TOCReference:tocReferences){
StringBuilder tocstring=新的StringBuilder();
对于(int i=0;i使用此代码,前提是您在资产中有必要的库和samplepubfile.epub
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.webkit.WebView;
public class EPubDemo extends Activity {
WebView webview;
String line, line1 = "", finalstr = "";
int i = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
AssetManager assetManager = getAssets();
try {
// find InputStream for book
InputStream epubInputStream = assetManager
.open("sampleepubfile.epub");
// Load Book from inputStream
Book book = (new EpubReader()).readEpub(epubInputStream);
// Log the book's authors
Log.i("author", " : " + book.getMetadata().getAuthors());
// Log the book's title
Log.i("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 exception", e.getMessage());
}
String javascrips = "";
try {
// InputStream input = getResources().openRawResource(R.raw.lights);
InputStream input = this.getAssets().open(
"poe-fall-of-the-house-of-usher.epub");
int size;
size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
javascrips = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
// String html = readFile(is);
webview.loadDataWithBaseURL("file:///android_asset/", javascrips,
"application/epub+zip", "UTF-8", null);
}
@SuppressWarnings("unused")
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("TOC", tocString.toString());
try {
InputStream is = tocReference.getResource().getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
while ((line = r.readLine()) != null) {
// line1 = Html.fromHtml(line).toString();
Log.v("line" + i, Html.fromHtml(line).toString());
// line1 = (tocString.append(Html.fromHtml(line).toString()+
// "\n")).toString();
line1 = line1.concat(Html.fromHtml(line).toString());
}
finalstr = finalstr.concat("\n").concat(line1);
// Log.v("Content " + i, finalstr);
i++;
} catch (IOException e) {
}
logTableOfContents(tocReference.getChildren(), depth + 1);
}
webview.loadDataWithBaseURL("", finalstr, "text/html", "UTF-8", "");
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.List;
导入nl.siegmann.epublib.domain.Book;
导入nl.siegmann.epublib.domain.TOCReference;
导入nl.siegmann.epublib.epub.EpubReader;
导入android.app.Activity;
导入android.content.res.AssetManager;
导入android.os.Bundle;
导入android.text.Html;
导入android.util.Log;
导入android.webkit.WebView;
公共类EPubDemo扩展活动{
网络视图;
字符串行,line1=“”,finalstr=“”;
int i=0;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(webview)findviewbyd(R.id.webview);
AssetManager AssetManager=getAssets();
试一试{
//查找书籍的输入流
InputStream epubInputStream=assetManager
.open(“samplepubfile.epub”);
//从inputStream加载图书
BookBook=(新的EpubReader()).readEpub(epubInputStream);
//记录这本书的作者
Log.i(“author”,:“+book.getMetadata().getAuthors());
//记录书名
Log.i(“title”,:“+book.getTitle());
/*记录书籍的封面图像属性*/
//位图封面图像=
//BitmapFactory.decodeStream(book.getCoverImage()
//.getInputStream());
//Log.i(“epublib”,“Coverimage是”+Coverimage.getWidth()+
//“由”
//+coverImage.getHeight()+“像素”);
//记录内容的故事
内容日志表(book.getTableOfContents().gettoReferences(),0);
}捕获(IOE异常){
Log.e(“epublib异常”,e.getMessage());
}
字符串javascrips=“”;
试一试{
//InputStream输入=getResources().openRawResource(R.raw.lights);
InputStream输入=this.getAssets().open(
《爱伦·坡·厄舍之家的倒塌》(epub);
整数大小;
size=input.available();
字节[]缓冲区=新字节[大小];
输入读取(缓冲区);
input.close();
//将字节缓冲区转换为字符串
javascrips=新字符串(缓冲区);
}捕获(IOE异常){
e、 printStackTrace();
}
//字符串html=readFile(is);
webview.loadDataWithBaseURL(“file:///android_asset/“,javascrips,
“应用程序/epub+zip”、“UTF-8”,空);
}
@抑制警告(“未使用”)
内容的私有void日志表(列表引用,int深度){
if(tocpreferences==null){
返回;
}
for(TOCReference TOCReference:tocReferences){
StringBuilder tocString=新的StringBuilder();
for(int i=0;i