2012-04-03 142 views
4

我正在使用http://www.siegmann.nl/epublib來讀取epub文件。我的代碼在下面提到。Android:Epub文件未在仿真器/ Android設備中顯示圖像

try { 
    book = epubReader.readEpub(new FileInputStream("/sdcard/EpubTesting.epub")); 

     Resource res; 
     Spine contents = book.getSpine(); 

     List<SpineReference> spinelist = contents.getSpineReferences(); 
     StringBuilder string = new StringBuilder(); 
     String line = null; 
     int count = spinelist.size(); 


     for (int i=0;i<count;i++){ 
      res = contents.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(); 
     } 
     } 


     System.out.println(linez); 
     s1.loadDataWithBaseURL("/sdcard/",linez, "text/html", "UTF-8",null); 

    }catch (FileNotFoundException e) { 

     Toast.makeText(mContext, "File not found.", Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     Toast.makeText(mContext, "IO Exception.", Toast.LENGTH_SHORT).show(); 
    } 

也試過

s1.loadDataWithBaseURL("",linez, "text/html", "UTF-8",null); 
s1.loadDataWithBaseURL("file://mnt/sdcard/",linez, "text/html", "UTF-8",null); 

但結果是sifar。請告訴我我必須做什麼來顯示文件中包含的圖像。我已經通過常見問題解答說讓一個android.webkit.WebView的子類重載loadUrl(String)方法,以便從圖書加載圖像而不是互聯網。但直到我不在他們提取文件的地方,我怎麼能找到路徑。請告訴我。我很困擾。提前致謝。

回答

3

爲此,您必須在sdcard中下載.epub文件的位置下載epub文件(即圖像,樣式表)的所有資源。請檢查以下代碼,使用epublib從.epub文件本身下載圖像和css文件。 因爲你必須發送File對象的參數來存儲這些圖像。

private void DownloadResource(File FileObj,String filename) { 
    try { 
    InputStream epubis = new FileInputStream(FileObj); 
    book = (new EpubReader()).readEpub(epubis); 

    Resources rst = book.getResources(); 
    Collection<Resource> clrst = rst.getAll(); 
    Iterator<Resource> itr = clrst.iterator(); 

    while (itr.hasNext()) { 
    Resource rs = itr.next(); 

    if ((rs.getMediaType() == MediatypeService.JPG) 
     || (rs.getMediaType() == MediatypeService.PNG) 
     || (rs.getMediaType() == MediatypeService.GIF)) { 
    File oppath1 = new File(directory, "Images/" 
     + rs.getHref().replace("Images/", "")); 

    oppath1.getParentFile().mkdirs(); 
    oppath1.createNewFile(); 

    FileOutputStream fos1 = new FileOutputStream(oppath1); 
    fos1.write(rs.getData()); 
    fos1.close(); 

    } else if (rs.getMediaType() == MediatypeService.CSS) { 
    File oppath = new File(directory, "Styles/" 
     + rs.getHref().replace("Styles/", "")); 

    oppath.getParentFile().mkdirs(); 
    oppath.createNewFile(); 

    FileOutputStream fos = new FileOutputStream(oppath); 
    fos.write(rs.getData()); 
    fos.close(); 

    } 

    } 


    } catch (Exception e) { 
    Log.v("error", e.getMessage()); 
    } 
} 

此後使用此代碼來設置webview中的圖像路徑。 如果儲存在SD卡中,然後

s1.loadDataWithBaseURL("file:///sdcard/",linez, "text/html",null,null); 

s1.loadDataWithBaseURL("file://mnt/sdcard/",linez, "text/html", "UTF-8",null); 

如果內部存儲然後

s1.loadDataWithBaseURL("file:///data/data/com.example.project/app_mydownload/",linez, "text/html",null,null); 
13
public class EpubBookContentActivity extends Activity{ 

private static final String TAG = "EpubBookContentActivity"; 
WebView webview; 

Book book; 

int position = 0; 

String line; 
int i = 0; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content); 

    webview = (WebView) findViewById(R.id.webView); 
    webview.getSettings().setJavaScriptEnabled(true); 

    AssetManager assetManager = getAssets(); 
    String[] files; 

    try { 

     files = assetManager.list("books"); 
     List<String> list =Arrays.asList(files); 

     if (!this.makeDirectory("books")) { 
      debug("faild to make books directory"); 
     } 

     copyBookToDevice(list.get(position)); 

     String basePath = Environment.getExternalStorageDirectory() + "/books/"; 

     InputStream epubInputStream = assetManager.open("books/"+list.get(position)); 

     book = (new EpubReader()).readEpub(epubInputStream); 

     DownloadResource(basePath); 

     String linez = ""; 
     Spine spine = book.getSpine(); 
     List<SpineReference> spineList = spine.getSpineReferences() ; 
     int count = spineList.size(); 

     StringBuilder string = new StringBuilder(); 
     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 { 
        while ((line = reader.readLine()) != null) { 
         linez = string.append(line + "\n").toString(); 
        } 

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


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

     } 

     linez = linez.replace("../", ""); 

//   File file = new File(Environment.getExternalStorageDirectory(),"test.html"); 
//   file.createNewFile(); 
//   FileOutputStream fileOutputStream = new FileOutputStream(file); 
//   fileOutputStream.write(linez.getBytes()); 
//   fileOutputStream.close(); 


     webview.loadDataWithBaseURL("file://"+Environment.getExternalStorageDirectory()+"/books/", linez, "text/html", "utf-8", null); 

    } catch (IOException e) { 
     Log.e("epublib exception", e.getMessage()); 
    } 
} 

public boolean makeDirectory(String dirName) { 
    boolean res;   

    String filePath = new String(Environment.getExternalStorageDirectory()+"/"+dirName); 

    debug(filePath); 
    File file = new File(filePath); 
    if (!file.exists()) { 
     res = file.mkdirs(); 
    }else { 
     res = false;   
    } 
    return res; 
} 

public void debug(String msg) { 
    //  if (Setting.isDebug()) { 
    Log.d("EPub", msg); 
    //  } 
} 

public void copyBookToDevice(String fileName) {  
    System.out.println("Copy Book to donwload folder in phone"); 
    try 
    { 
     InputStream localInputStream = getAssets().open("books/"+fileName); 
     String path = Environment.getExternalStorageDirectory() + "/books/"+fileName; 
     FileOutputStream localFileOutputStream = new FileOutputStream(path); 

     byte[] arrayOfByte = new byte[1024]; 
     int offset; 
     while ((offset = localInputStream.read(arrayOfByte))>0) 
     { 
      localFileOutputStream.write(arrayOfByte, 0, offset);     
     } 
     localFileOutputStream.close(); 
     localInputStream.close(); 
     Log.d(TAG, fileName+" copied to phone"); 

    } 
    catch (IOException localIOException) 
    { 
     localIOException.printStackTrace(); 
     Log.d(TAG, "failed to copy"); 
     return; 
    } 
} 



private void DownloadResource(String directory) { 
    try { 

     Resources rst = book.getResources(); 
     Collection<Resource> clrst = rst.getAll(); 
     Iterator<Resource> itr = clrst.iterator(); 

     while (itr.hasNext()) { 
      Resource rs = itr.next(); 

      if ((rs.getMediaType() == MediatypeService.JPG) 
              || (rs.getMediaType() == MediatypeService.PNG) 
              || (rs.getMediaType() == MediatypeService.GIF)) { 

       Log.d(TAG, rs.getHref()); 

       File oppath1 = new File(directory, rs.getHref().replace("OEBPS/", ""));  

       oppath1.getParentFile().mkdirs(); 
       oppath1.createNewFile(); 

       System.out.println("Path : "+oppath1.getParentFile().getAbsolutePath()); 


       FileOutputStream fos1 = new FileOutputStream(oppath1); 
       fos1.write(rs.getData()); 
       fos1.close(); 

      } else if (rs.getMediaType() == MediatypeService.CSS) { 

       File oppath = new File(directory, rs.getHref()); 

       oppath.getParentFile().mkdirs(); 
       oppath.createNewFile(); 

       FileOutputStream fos = new FileOutputStream(oppath); 
       fos.write(rs.getData()); 
       fos.close(); 

      } 

     } 


    } catch (Exception e) { 

    } 
} 
} 
+0

由於一噸格爾登,它的一個很好的示範。謝謝。但這裏有一個圖像沒有顯示的問題。你有關於這個問題的更新嗎? – 2013-12-30 12:09:08

+1

css&圖像被下載到存儲/ sdcard0/books/images&..same path/css但不顯示在屏幕上。 css已成功應用。僅限圖片發行 – 2013-12-30 12:33:01

+0

將圖書數據存儲到文件中。只在我的代碼中刪除評論。並將images和test.html文件放在同一個文件夾中,並在test.html中檢查問題 – 2013-12-30 12:43:16

相關問題