2012-04-25 80 views
1

我们通过我们的应用程序在屏幕上显示epub文件。该文件保存在SDCard中,以及我们用于从SDCard获取文件数据并在屏幕中显示的以下逻辑。但花费很长时间才能在屏幕上加载内容。我的代码的任何问题?请帮助我的朋友。花费很长时间在设备上显示epub文件

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

} 

请帮助我的朋友。

回答

2

一个ePub本质上只不过是一个带有大量HTML文件的zip文件。通常,本书的每章/部分将会有一个文件(资源)。

你现在正在做的是通过书脊循环,加载所有的资源时,你也许可以一次显示1顶多在屏幕上。

我建议只加载你想要显示的资源,这会显着加快加载速度。

+0

任何人都可以给我链接样本EPUB阅读器? – 2012-05-10 07:01:54

+0

有可能再次发布我自己的项目,请看看:http://github.com/nightwhistler/pageturner - 请注意,它是GPL许可的。 – NightWhistler 2012-05-11 08:34:51

2

首先你没有正确使用StringBuilder的 - 这是在你的代码完全无用。其次,决定你是否真的需要嵌套的try-catch块。第三,定义循环外的局部变量。关于这一切我已经重写你的代码是这样的:

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

不过,我想,这不会大幅减少加载您的内容所需要的时间,所以我建议你使用Traceview找到代码中的瓶颈和使用AsyncTask进行耗时的操作。

相关问题