【问题标题】:Android: Epub file not showing images in emulator/android deviceAndroid:Epub 文件未在模拟器/Android 设备中显示图像
【发布时间】:2012-04-16 22:34:48
【问题描述】:

我正在使用http://www.siegmann.nl/epublib 来读取 epub 文件。我的代码在下面提到。

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) 方法,以便从书而不是互联网加载图像。但是直到我不知道他们提取文件的位置,我怎样才能找到路径。请告诉我。我很迷茫。提前致谢。

【问题讨论】:

    标签: java android android-layout android-emulator epub


    【解决方案1】:
    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) {
    
        }
    }
    }
    

    【讨论】:

    • 非常感谢 Kirti,这是一个不错的演示。谢谢。但这是图像未显示的一个问题。您对这个问题有任何更新吗?
    • css 和图像被下载到 storage/sdcard0/books/images & ..same path/css 但不显示在屏幕上。 css 已成功应用。仅图像问题
    • 将图书数据存储到文件中。只需删除我代码中的注释。并将图像和 test.html 文件放在同一文件夹中并检查问题
    • 在test.html中查看img src的路径
    • 我们真的需要将图像和样式保存到外部存储吗?我们不能直接访问它们吗?
    【解决方案2】:

    为此,您必须在 sdcard 中下载 .epub 文件的位置下载所有 epub 文件资源(即图像、样式表)。请检查以下代码以使用 epublib 从 .epub 文件本身下载图像和 css 文件。 为此,您必须发送要存储这些图像的文件对象的参数。

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

    【讨论】:

      猜你喜欢
      • 2017-01-16
      • 2011-02-16
      • 2017-08-06
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      相关资源
      最近更新 更多