Taking long time to display epub files in device

久未见 提交于 2019-12-08 05:17:05

问题


We are displaying an epub file on the screen through our application. The file is saved in SDCard and the following logic we are using for getting the file data from SDCard and displaying in Screen. But its taking long time to load the content in screen. Any issues with my code? please help me friends.

 File rootDir = Environment.getExternalStorageDirectory();
   EpubReader epubReader = new EpubReader();
   try {
        book = epubReader.readEpub(new FileInputStream("/sdcard/forbook.epub"));
        Toast.makeText(getApplicationContext(), "Book : " + book, Toast.LENGTH_LONG).show();
    } catch (FileNotFoundException e) {
        Toast.makeText(getApplicationContext(), "File Not Found" + book, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "IO Found" + book, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
   Spine spine = book.getSpine(); 
   List<SpineReference> spineList = spine.getSpineReferences() ;
   int count = spineList.size();
   StringBuilder string = new StringBuilder();
   String linez = null;
    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 {
               String line;
            while ((line = reader.readLine()) != null) {
                   linez =   string.append(line + "\n").toString();
                    //linez=line.toString();
               }

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

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

   }
  final String mimeType = "text/html";
  final String encoding = "UTF-8";
  webView.loadDataWithBaseURL("", linez, mimeType, encoding,null);

}

Please help me friends.


回答1:


An ePub is essentially nothing more than a zip file with a number of HTML files inside. Often, there will be one file (resource) per chapter / section of the book.

What you're doing right now is looping through the spine of the book, loading all the resources when you can probably display 1 at most on the screen at a time.

I'd suggest only loading the resource you want to show, which should speed up the loading time dramatically.




回答2:


First of all you do not use StringBuilder correctly - it's quite useless in your code. Secondly, decide if you really need nested try-catch block. Thirdly, define local variables outside the loops. Concerning all of this I'd rewrite your code this way:

    StringBuilder string = new StringBuilder();
    Resource res;
    InputStream is;
    BufferedReader reader;
    String line;
    for (int i = 0; count > i; i++) {
        res = spine.getResource(i);
        try {
            is = res.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is));
            while ((line = reader.readLine()) != null) {
                string.append(line + "\n");
            }

            // do something with stream
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    ...
    webView.loadDataWithBaseURL("", string.toString(), mimeType, encoding, null);

However, I suppose, this wouldn't drastically reduce the time needed to load your content, so I'd recommend you to use Traceview to find the bottleneck in your code and to use AsyncTask for time-consuming operations.



来源:https://stackoverflow.com/questions/10313113/taking-long-time-to-display-epub-files-in-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!