2012-04-25 79 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進行耗時的操作。

相關問題