How to combine cover page and chapter contents of epublib in single WebView?

90 Views Asked by At

So, I'm trying to load the cover page and the chapter contents(in its respective sequence) within a single WebView using Epublib. So far, what I've done ... I retrieved my epub file from the cloud and unzipped in using the epublib library using the following code:

try {
    EpubReader epubReader = new EpubReader();
    Book book = epubReader.readEpub(inputStream);
    inputStream.close();
    return book;
} catch (IOException e) {
    Log.e("Exception: ", e.toString());
}

Upon unzipping, I have access to cover.xhtml and chapter-1.xhtml (along with some other files) which I write to a file inside temp directory:

private File writeFile(File tempFile, String fileData) throws IOException {
    BufferedWriter bWriter = new BufferedWriter(new FileWriter(tempFile));
    bWriter.write(fileData);
    tempFile.deleteOnExit();
    return tempFile;
}

Now, what I've tried was import the cover.xhtml page inside the chapter-1.xhtml page assuming that it would load in a single WebView.

//get file to load on webview
File file = new File(contentFilePath);

// Check if content file exists or not
if (!file.exists()) { System.out.println("Does not exist");}
else { 
    System.out.println("Exists");
    
    String contentData = readFile(file);
    Document doc = Jsoup.parse(contentData);
    doc.head().prependElement("link").attr("rel", "import").attr("href","cover.xhtml");
    webView.loadDataWithBaseURL("file://"+tempDir.getAbsolutePath()+"/", contentData, "application/xhtml+xml", "UTF-8", null);

}

However, this doesn't seem to load the cover page although the contents page is showing. Any help is much appreciated.

0

There are 0 best solutions below