Taking long time to display epub files in device

二次信任 提交于 2019-12-07 22:45:31

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.

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.

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