Search code examples
androidepub

Blank Screen and only pop up "Not Responding" dialog.


I am unable to figure out why I am stuck at this point although there is no error is my coding. My problem is actually, it doesn't print out my epub, as well as it just hangs there, till the not responding dialog comes up.

My code is as below:

            package com.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.Resource;
    import nl.siegmann.epublib.domain.Spine;
    import nl.siegmann.epublib.domain.SpineReference;
    import nl.siegmann.epublib.epub.EpubReader;

    import android.app.Activity;
    import android.content.res.AssetManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.webkit.WebView;
    import android.widget.TextView;

    public class EpubReaderActivity extends Activity {
        WebView webView;
        Book book;
        TextView tv;
        String line;
        String linez;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            tv = new TextView(this);
            webView = (WebView)findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            AssetManager am = getAssets();
            try {
                InputStream epubInputStream = am.open("testbook.epub");
                book = (new EpubReader()).readEpub(epubInputStream);
            } catch (IOException e) {
                Log.e("epublib", e.getMessage());
            }
            Spine spine = book.getSpine(); 
            List<SpineReference> spineList = spine.getSpineReferences() ;
            int count = spineList.size();
            tv.setText(Integer.toString(count));
            StringBuilder string = new StringBuilder();
            for (int i = 0; count > i; i++) {
                Resource 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();}

                    //do something with stream
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            webView.loadData(linez, "text/html", "utf-8");

        }
    }

Mind helping me out on this point?


Solution

  • Pu this code inside a thread as this takes time to write the data and load a web view Thread is a different part of execution and you will have to wait for few sconds if yor file is large you can also put a progress dialog to show loading of webview

               new Thread(){
            public void run() {
                        for (int i = 0; count > i; i++) {
                        Resource 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();}
    
                    //do something with stream
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
    
         };
        }.start();