java– 如何在android中读取Epub文件的所有内容?

java– 如何在android中读取Epub文件的所有内容?,第1张

概述我想阅读所有toc的所有内容.您可以告诉我如何阅读内容表(toc)中的所有章节.请发布修改后的代码,以便我们可以阅读所有内容或发布一些内容,以便我可以阅读内容.publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setConte

我想阅读所有toc的所有内容.您可以告诉我如何阅读内容表(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);        }               }                                         }

解决方法:

如果您的资产中有必要的库和sampleepubfile.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 – 如何在android中读取Epub文件的所有内容?全部内容,希望文章能够帮你解决java – 如何在android中读取Epub文件的所有内容?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105896.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存