如何阅读EPUB文件的android系统中的所有内容? [英] How to read all content of Epub file in android?

查看:180
本文介绍了如何阅读EPUB文件的android系统中的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的所有TOC的所有内容。
u能请告诉我如何读取所有章,其中有在表中的内容(TOC)的。请发布修改code,这样我们就可以阅读所有内容,或发布一些东西,这样我可以读的内容。

 公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
       AssetManager assetsmanger = getAssets();

        尝试 {
             //找到的InputStream的书
                InputStream的epubInputStream = assetsmanger.open(书籍/所得税-ACT-1961.epub);

              从输入流//装载图书
              书书=(新EpubReader())readEpub(epubInputStream)。

            //登录这本书的作者
              Log.i(epublib,作者(S):+ book.getMetadata()getAuthors());

                //登录书的标题
             Log.i(epublib,标题:+ book.getTitle());

        字符串数据=新的String(book.getContents()得到(3).getData());
         字符串K =数据;
                web1的=(的WebView)findViewById(R.id.webView1);
// TXT1 =(TextView中)findViewById(R.id.textView1);
// txt1.setText(K);
            web1.loadData(K,text / html的,UTF8);

        位图coverImage = BitmapFactory.de codeStream(book.getCoverImage()的getInputStream());

         Log.i(epublib,Coverimage为+ coverImage.getWidth()+由
                  + coverImage.getHeight()+像素);
        //日志内容的故事
         logTableOfContents(book.getTableOfContents()getTocReferences(),0);

    //名单=(ExpandableListView)findViewById(R.id.expandableListView1);


// IMG1 =(ImageView的)findViewById(R.id.imageView1);
// img1.setImageBitmap(coverImage);
            }赶上(IOException异常E){
                // TODO自动生成的catch块
                 Log.e(epublib,e.getMessage());
            }
            赶上(例外五){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
        }

    私人无效logTableOfContents(名单< TOCReference> tocReferences,诠释深度){
        // TODO自动生成方法存根

        如果(tocReferences == NULL){
            返回;
        }
        对于(TOCReference tocReference:tocReferences){
            StringBuilder的tocstring =新的StringBuilder();
            的for(int i = 0; I<深度;我++)
            {
                tocstring.append(\ t的);

            }
            HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();


        串K = tocstring.append(tocReference.getTitle())的toString()。
        map.put(TOC,k)的;
        ArrayList的< HashMap的<字符串,字符串>> List1中=新的ArrayList< HashMap的<字符串,字符串>>();
        list1.add(图)
        串T = K;
             Log.i(epublib,tocstring.toString());
             logTableOfContents(tocReference.getChildren(),深度+ 1);

        }
    }
    }
 

解决方案

使用这种code只要你有必要的库和 sampleepubfile.epub 资产 ...

 进口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延伸活动{
    的WebView的WebView;
    串线,一号线=,finalstr =;
    INT I = 0;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        的WebView =(web视图)findViewById(R.id.webview);
        AssetManager assetManager = getAssets();
        尝试 {
            //找到的InputStream的书
            InputStream的epubInputStream = assetManager
                    。开(sampleepubfile.epub);

            从输入流//装载图书
            书书=(新EpubReader())readEpub(epubInputStream)。

            //登录这本书的作者
            Log.i(作家,:+ book.getMetadata()getAuthors());

            //登录书的标题
            Log.i(标题,:+ book.getTitle());

            / *日志书的coverimage财产* /
            //位图coverImage =
            // BitmapFactory.de codeStream(book.getCoverImage()
            // .getInputStream());
            // Log.i(epublib,Coverimage为+ coverImage.getWidth()+
            //  通过 
            // + coverImage.getHeight()+像素);

            //日志内容的故事
            logTableOfContents(book.getTableOfContents()getTocReferences(),0);
        }赶上(IOException异常E){
            Log.e(epublib异常,e.getMessage());
        }

        字符串javascrips =;
        尝试 {
            // InputStream中输入= getResources()openRawResource(R.raw.lights)。
            输入的InputStream = this.getAssets()。打开(
                    POE-跌落 - 的 - 房子的-usher.epub);

            INT大小;
            大小= input.available();
            byte []的缓冲区=新的字节[尺寸]
            input.read(缓冲液);
            input.close();
            //字节的缓冲区转换为字符串
            javascrips =新的String(缓冲区);
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        //字符串的HTML = READFILE(是);

        webview.loadDataWithBaseURL(文件:/// android_asset /,javascrips,
                应用程序/ EPUB +压缩,UTF-8,NULL);
    }

    @燮pressWarnings(未使用)
    私人无效logTableOfContents(名单< TOCReference> tocReferences,诠释深度){
        如果(tocReferences == NULL){
            返回;
        }

        对于(TOCReference tocReference:tocReferences){
            StringBuilder的tocString =新的StringBuilder();
             的for(int i = 0; I<深度;我++){
             tocString.append(\ t的);
             }
             tocString.append(tocReference.getTitle());
             Log.i(TOC,tocString.toString());

            尝试 {
                InputStream的是= tocReference.getResource()的getInputStream()。
                BufferedReader中R =新的BufferedReader(新InputStreamReader的(是));

                而((行= r.readLine())!= NULL){
                    // 1行= Html.fromHtml(线)的ToString();
                    Log.v(线+ I,Html.fromHtml(线)的ToString());
                    // 1行=(tocString.append(Html.fromHtml(线)的ToString()+
                    //\ N))的toString()。
                    第1行= line1.concat(Html.fromHtml(线)的ToString());
                }
                。finalstr = finalstr.concat(\ N)CONCAT(一号线);
                // Log.v(内容+ I,finalstr);
                我++;
            }赶上(IOException异常E){

            }

            logTableOfContents(tocReference.getChildren(),深度+ 1);
        }
        webview.loadDataWithBaseURL(,finalstr,text / html的,UTF-8,);
    }
}
 

I want to read all content of all toc.
Can u please tell me how to read all chapter which is there in table of content(toc ).
Please post modified code so that we can read all content or post some thing so that i can read content .

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

        }           
    }                                     
    }

解决方案

Use this code provided you have necessary libraries and sampleepubfile.epub in your assets...

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", "");
    }
}

这篇关于如何阅读EPUB文件的android系统中的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆