如何使用EPUBLIB阅读EPUB书?

时间:2011-07-01 09:34:22

标签: android epub epublib

我找到了一个使用epublib在android中阅读epub书籍的解决方案。我能够阅读这本书的字幕。但我没有找到一种方法来逐行阅读内容。我怎么能实现这个目标呢?

获取图书标题的示例代码是

  private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
    if (tocReferences == null) {
        return;
    }
    for (TOCReference tocReference : tocReferences) {
        StringBuilder tocString = new StringBuilder();
        StringBuilder tocHref=new StringBuilder();
        for (int i = 0; i < depth; i++) {
            tocString.append("\t");
            tocHref.append("\t");
        }
        tocString.append(tocReference.getTitle());

        tocHref.append(tocReference.getCompleteHref());
        Log.e("Sub Titles", tocString.toString());
        Log.e("Complete href",tocHref.toString());

        //logTableOfContents(tocReference.getChildren(), depth + 1);
    }
}

从[{3}}

获取此代码

我怎样才能得到这本书的故事......

3 个答案:

答案 0 :(得分:7)

我不确定这是在epub文件中导航的方法。据我所知(直到现在 - 我还在学习),更好的方法来获得所有书籍cocntent是基于脊柱部分。 但仍然 - 我不知道如何将这两件事(TOC和真正的脊椎)与epublib接口连接起来。 根据文件: “脊柱部分是本书的各个部分,按照阅读本书的顺序。这与目录部分形成对比,这是本书部分的索引。”

这是某种东西 - 如果你喜欢 - 这是一个片段:

Spine spine = new Spine(book.getTableOfContents());
for (SpineReference bookSection : spine.getSpineReferences()) {
            Resource res = bookSection.getResource();
                try {
                    InputStream is = res.getInputStream();
                    //do something with stream
                } catch (IOException e) {

答案 1 :(得分:3)

嗯 - 我不确定导航,但也想知道如何做到这一点 现在 - 我有类似的东西(逐行阅读):

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");
        }
        try{
           InputStream is = tocReference.getResource().getInputStream();
           BufferedReader r = new BufferedReader(new InputStreamReader(is));
           String line;
           while ((line = r.readLine()) != null) {
               String line = Html.fromHtml(line).toString();
           }
        }
        catch(IOException e){

        }




        //logTableOfContents(tocReference.getChildren(), depth + 1);
    }
}

答案 2 :(得分:2)

我尝试使用此代码逐行读取epub文件中的所有文本,但它不能正常工作! 我怎样才能做到这一点 ?

package com.mahdi.File_Reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.jar.JarFile;

import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.AnalogClock;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.mahdi.File_Reader.jchmlib.*;

import nl.siegmann.epublib.*;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;

public class File_ReaderActivity extends Activity {



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv1 = (TextView) findViewById(R.id.textView1);

    AssetManager assetManager = getAssets();

    InputStream inputStream;
    try {
        inputStream = assetManager.open("xxx.epub");
        Book ebook = new EpubReader().readEpub(inputStream);

  tv1.setText(logTableOfContents(ebook.getTableOfContents().getTocReferences(), 0));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

private String logTableOfContents(List<TOCReference> tocReferences,
        int depth) {
    String lineXX="p";
    if (tocReferences == null) {
        return "d";
    }
    for (TOCReference tocReference : tocReferences) {
        StringBuilder tocString = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            tocString.append("\t");
        }
        try {

    InputStream is =  tocReference.getResource().getInputStream();

    BufferedReader r = new BufferedReader(new  InputStreamReader(is));
            String line;
            while ((line = r.readLine()) != null) {
                String lineX = Html.fromHtml(line).toString();
                lineXX=lineX;
            }
        } catch (IOException e) {
              lineXX="X";
        }



        // logTableOfContents(tocReference.getChildren(), depth + 1);
     }
     return lineXX;
 }

}