无法在屏幕上显示目录或整本书

时间:2014-01-14 04:38:12

标签: android android-layout epub

我正在使用siegmann epub库创建一个书籍阅读器。我无法在屏幕上显示任何内容,但在logcat中。什么类型的布局用于显示?

这是我的main.java

public class MainActivity extends Activity {

    private WebView webview;
    private String line, line1="", finalstr="";
    int i = 0;
    private String fullBook;
    private Book book;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         webview = (WebView) findViewById(R.id.webview);
        AssetManager assetManager = getAssets();

        try{
            //find input Stream for book
            InputStream epubInputStream = assetManager.open("oliver_twist.epub");

            //Load book from input stream
            book = (new EpubReader()).readEpub(epubInputStream);

            Log.i("epublib", "title: "+book.getTitle());

            //Log the book's cover image property
            //Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());

            //Log.i("epublib", "CoverImage is" + coverImage.getWidth()+" by "+coverImage.getHeight()+" pixels");

            //Log the tables of contents
            logTableOfContents(book.getTableOfContents().getTocReferences(),0);

            fullBook = getEntireBook();


        }
        catch(IOException e){
            Log.e("epublib", 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);
        //webview.setwe
    }

    private void logTableOfContents(List<TOCReference> tocReferences, int depth){

        if(tocReferences == null){
            System.out.println("---->>>"+tocReferences);
            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("epulib", 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.i("Content " + i, finalstr);
                    i++;
                } catch (IOException e) {

                }


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

    public String getEntireBook(){
        String line, linez = null;
        Spine spine = book.getSpine();
        Resource res;
        List<SpineReference> spineList = spine.getSpineReferences() ;

        int count = spineList.size();
        int start = 0;

        StringBuilder string = new StringBuilder();
        for (int i = start; count > i; i = i +1) {
            res = spine.getResource(i);

                try {
                    InputStream is = res.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    try {
                        while ((line = reader.readLine()) != null) {
                            linez =   string.append(line + "\n").toString();
                        }

                    } catch (IOException e) {e.printStackTrace();}
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return linez;


    }

}

在main.xml中,我刚创建了一个webview。我可以添加哪些电影?它是显示哪本书的文本视图吗?

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了它并且它有效!!

public class MainActivity extends Activity {

    private WebView webview;
    private String line, line1="", finalstr="";
    int i = 0;
    private String fullBook;
    private Book book;

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings =  webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        AssetManager assetManager = getAssets();
        //tv = (TextView) findViewById(R.id.tv);

        try{
            //find input Stream for book
            InputStream epubInputStream = assetManager.open("TheThreeBears.epub");

            //Load book from input stream
            book = (new EpubReader()).readEpub(epubInputStream);

            Log.i("epublib", "title: "+book.getTitle());

            //Log the book's cover image property
            //  Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());

            //Log.i("epublib", "CoverImage is" + coverImage.getWidth()+" by "+coverImage.getHeight()+" pixels");

            //Log the tables of contents
            logTableOfContents(book.getTableOfContents().getTocReferences(),0);

            getEntireBook();


        }
        catch(IOException e){
            Log.e("epublib", e.getMessage());
        }

    }

    private void logTableOfContents(List<TOCReference> tocReferences, int depth){
        // Load entire text into particular no. of lines and display each
        // then with next button navigate through pages

        String lineXX = "p";
        if(tocReferences == null){
            System.out.println("---->>>"+tocReferences);
            return ;
        }

        for(TOCReference tocReference:tocReferences){

            StringBuilder tocString = new StringBuilder();

            for(int i=0;i<depth;i++){
                tocString.append("\t");
            }

            tocString.append(tocReference.getTitle());
            //tv.setText(tocString.toString());
            //Log.i("epulib", tocString.toString());

            try {
                InputStream is = tocReference.getResource().getInputStream();
                BufferedReader r = new BufferedReader(new InputStreamReader(is));

                while ((line = r.readLine()) != null) {

                    //String lineX =  Html.fromHtml(line).toString();
                    //lineXX = lineX;
                    Log.v("line" + i, Html.fromHtml(line).toString());

                    line1 = line1.concat(Html.fromHtml(line).toString());

                }
                finalstr = finalstr.concat("\n").concat(line1);
                //tv.setText(finalstr);
                //Log.i("Content " + i, finalstr);
                i++;
            } catch (IOException e) {

            }


            logTableOfContents(tocReference.getChildren(), depth+1);
        }
        //return lineXX;
        webview.loadDataWithBaseURL("", finalstr, "text/html", "UTF-8", "");
    }

    public void getEntireBook(){
        String line, linez = null;
        Spine spine = book.getSpine();
        Resource res;
        List<SpineReference> spineList = spine.getSpineReferences() ;

        int count = spineList.size();
        int start = 0;

        StringBuilder string = new StringBuilder();
        for (int i = start; count > i; i = i +1) {
            res = spine.getResource(i);

            try {
                InputStream is = res.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                try {
                    while ((line = reader.readLine()) != null) {
                        linez =   string.append(line + "\n").toString();
                    }

                } catch (IOException e) {e.printStackTrace();}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        webview.loadData(linez, "text/html", "utf-8");

    }

}