Using ePubLib for Android

I’m rewriting the Examine the Scriptures Daily android app. With the ESD files published as ePub files it’s easier to get the data now, and it will allow more languages to be supported without the hassle of converting from WTLib.

Here’s the code snippet I’m using to read the ePub book. The library I’m using is ePubLib, and I’m displaying the page in a standard WebView:

    // Load the book and get a specific date
    AssetManager assetManager = getAssets();
    try {
		// find InputStream for book
		InputStream epubInputStream = assetManager.open("es12_E.epub");
		// Load Book from inputStream
		this.book = (new EpubReader()).readEpub(epubInputStream);
		getSpecificdate("2012-12-1");
	} catch (IOException e) {
		Log.e("epublib", e.getMessage());
	}

    private void getSpecificdate(String day) { 
		Date d; 
		int dayOfMonth;
		int monthOfYear;
		Calendar cal = Calendar.getInstance();
		try {
			d = dateFormat.parse(day);
			cal.setTime(d);
			String hrefDate = hrefFormat.format(d).toUpperCase();
			dayOfMonth=cal.get(Calendar.DAY_OF_MONTH);
			monthOfYear=cal.get(Calendar.MONTH);
			hrefDate += (dayOfMonth==1) ? ".xhtml" : "-split"+dayOfMonth+".xhtml";
			hrefDate = dblzero.format(monthOfYear+4)+hrefDate.substring(2);
			
            // Here's the part you're looking for
			Resource r = this.book.getResources().getByHref(hrefDate);
			WebView wv = (WebView) findViewById(R.id.webView1);
			String data = new String( r.getData() );
			wv.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "utf-8", null);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}

Similar Posts:

Tagged , , , , , ,

6 thoughts on “Using ePubLib for Android

  1. Fabiano says:

    ok, it’s great but I’m trying to use epub but my page have images that it’s not show and the css is not applied. Can you help me?

    • abdallah says:

      Hi,
      I’m not sure about the images really. But for the CSS, I had to extract that to the assets folder to apply it!
      Make sure to follow the debug output to find your exact error 🙂

    • Abdallah says:

      Following up. Here’s some info from the FAQ over at http://www.siegmann.nl/epublib/faq
      How do I display a book’s images on Android ?
      Make a subclass of android.webkit.WebView that overloads the loadUrl(String) method in such a way that it loads the image from the Book instead of the internet.

      The code would make use of something like:
      Bitmap coverImage = BitmapFactory.decodeStream(imageResource);

      • Carlos says:

        Hi,

        Could you explain this? How do I make the images work by making a subclass of webview and overridding loadUrl? Can you paste a sample code for me? Thanks!

  2. omid says:

    Ok, thanks for write this article. But I wana know that how do you add EPublib to project. also, Do you use a slf4j library for logs?

  3. abdallah says:

    @omid Please check http://www.siegmann.nl/epublib/android for more info.
    Basically you just need to add the 2 jar files to your project (as libraries).
    If you’re using eclipse: Go to Project Properties -> Java Build Path -> Libraries tab and click on Add Jars …

Leave a Reply

Your email address will not be published. Required fields are marked *