문제

I am creating a book reader using siegmann epub library. I am not able to display anything on the screen but in the logcat. What type of layout to use for display?

Here is my 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;


    }

}

In main.xml, I have just created a webview. What otehr elemnets I can add? Is it a text view on which book is displayed?

도움이 되었습니까?

해결책

I solved it using the following code and it worked!!

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

    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top