2012-10-19 6 views
5

का उपयोग कर छवियों को प्रदर्शित करना मैं वेबव्यू में एक .epub फ़ाइल पढ़ने के लिए epublib का उपयोग कर रहा हूं।EPUBLIB

WebView wv = (WebView) getView().findViewById(R.id.chaptercontent); 
    try { 
     String abspath = FILEPATH+file; 
     File filePath = new File(abspath+".epub"); 
     InputStream epubInputStream = new BufferedInputStream(new FileInputStream(filePath)); 
     book = (new EpubReader()).readEpub(epubInputStream); 
     int pos = abspath.lastIndexOf('/'); 
     DownloadResource(abspath.substring(0, pos)); 
     try { 
      for(int i = 1; i< book.getContents().size(); i++) { 
       InputStream is = book.getSpine().getSpineReferences().get(i).getResource().getInputStream(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
        Log.d("display line", line); 
       } 
       is.close(); 
       wv.loadDataWithBaseURL(abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null); 
      } 
     } 
     catch(IOException e) { 
      Log.e("IOException", e.getMessage()); 
     } 
    } 
    catch (IOException e) { 
     Log.e("epublib", e.getMessage()); 
    } 

private void DownloadResource(String directory) { 
    try { 
     nl.siegmann.epublib.domain.Resources rst = book.getResources(); 
     Collection<Resource> clrst = rst.getAll(); 
     Iterator<Resource> itr = clrst.iterator(); 
     Log.d("Downlod path", directory); 
     while (itr.hasNext()) { 
      Resource rs = itr.next(); 
      if ((rs.getMediaType() == MediatypeService.JPG) || (rs.getMediaType() == MediatypeService.PNG) || (rs.getMediaType() == MediatypeService.GIF) || rs.getMediaType() == MediatypeService.CSS) { 
       File oppath1 = new File(directory+File.separator+rs.getHref()); 
       Log.d("Resource Name - ", rs.getHref()); 
       oppath1.createNewFile(); 
       Log.d("Oppath - ", oppath1.getAbsolutePath()); 

       Log.d("File Checking - ", "Exists - "+oppath1.exists()+" & Write - "+oppath1.canWrite()); 
       FileOutputStream fos1 = new FileOutputStream(oppath1); 
       fos1.write(rs.getData()); 
       fos1.close(); 

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

डाउनलोड संसाधन ठीक काम करता है। संसाधन प्राप्त किए जाते हैं। लेकिन वेबव्यू छवि प्रदर्शित नहीं कर रहा है। छवियां एपब फ़ाइल के समान निर्देशिका में हैं। उसके बाद

MediaType[] bitmapTypes = { MediatypeService.PNG, 
        MediatypeService.GIF, MediatypeService.JPG }; 
      List<Resource> resources = book.getResources().getResourcesByMediaTypes(bitmapTypes); 

आप पाश संसाधनों ही href फ़ाइल

private Bitmap getBitmapFromResources(List<Resource> resources, String imgHref) 
    { 
     byte[] data = "holder".getBytes(); 
     for(int ii = 0; ii < resources.size();ii++) 
     { 
      String z = resources.get(ii).getHref(); 
      if(z.equals(imgHref)) 
      { 
       Log.i("livi", z); 
       try { 
        data = resources.get(ii).getData(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       break; 
      } 
     } 
     // 
      Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); 
      return bm; 
    } 

प्राप्त करने के लिए कर सकते हैं और यह कैसे:

+0

क्या उस के लिए स्रोत की तरह काम किया पाठ (.xhtml फ़ाइल) जैसा दिखता है? – Freney

+1

क्या आप इसे हल करते हैं? – Villan

उत्तर

0

पहले आप सभी संसाधनों इस तरह मिल चाहिए: WebView मुझे इस देता है

Bitmap bm = getBitmapFromResources(resources, "cover.jpg"); 
      if (bm != null) 
       ivTest.setImageBitmap(bm); 

इस सहायता की आशा करें आप।

1

यह समस्या तब हुई है, लेकिन अगर किसी को इसका सामना करना पड़ा, तो मुझे एक ही समस्या का सामना करना पड़ा और समाधान बहुत आसान था जिसे अनदेखा कर दिया गया।

wv.loadDataWithBaseURL(abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null); 

इस लाइन abspath.substring(0, pos)+"/" कोड के इस हिस्से के साथ संसाधनों घर/वेबपेज मूल यूआरएल निर्दिष्ट करता है।

लेकिन हम यह प्रोटोकॉल, यानी HTTP, FTP, फ़ाइल (स्थानीय) तो ठीक उल्लेख नहीं था

wv.loadDataWithBaseURL("file://"+abspath.substring(0, pos)+"/", sb.toString(), "text/html", "utf-8", null); 

था और यह एक आकर्षण :)

+0

मेरा ईबुक नाम नमूना_बुक संपत्ति/एपब फ़ोल्डर के अंदर है। मुझे किस पूर्ण यूआरएल का उपयोग करना है? मुझे वर्तमान में यह त्रुटि मिल रही है 'संपत्ति यूआरएल खोलने में असमर्थ: फ़ाइल: /// android_asset/epub/sample_book/docimages/cover.jpg' –

+1

आपका पथ सही प्रतीत नहीं होता है, यह फाइल सिस्टम से शुरू होना चाहिए था। जैसे/डेटा/डाटा/YOUR_PACKAGE_NAME –