Monday 19 September 2011

Getting a Bitmap out of a Zip file in Android (or Java in general)

The following function will extract an image from a zip file stored anywhere on the filesystem (for as long as the application has access to it).

It will return a bitmap object if the image is found or null otherwise. This function should work in pure java as well with minor alterations (replacing the Log calls with system.outs).


public Bitmap getBtimapFromZip(final String zipFilePath, final String imageFileInZip){
	Log.d(TAG, "Getting image '" + imageFileInZip + "' from '" + zipFilePath +"'");
	Bitmap result = null;
	try {
		FileInputStream fis = new FileInputStream(zipFilePath);
		ZipInputStream zis = new ZipInputStream(fis);
		ZipEntry ze = null;
		while ((ze = zis.getNextEntry()) != null) {
			if (ze.getName().equals(imageFileInZip)) {
				result = BitmapFactory.decodeStream(zis);
				break;
			}
		}
	} catch (FileNotFoundException e) {
		Log.e(Constants.TAG, "Extracting file: Error opening zip file - FileNotFoundException: ", e);
		e.printStackTrace();
	} catch (IOException e) {
		Log.e(Constants.TAG, "Extracting file: Error opening zip file - IOException: ", e);
		e.printStackTrace();
	}
	return result;
}

If you you use a wrapper function like this:
private void loadImage(ImageButton button, String image){
	// Use this drawable by default
	Drawable d = context.getResources().getDrawable(R.drawable.no_image);
	Bitmap b = getBtimapFromZip("path_to_zip_file", image);
	
	if(b != null){ // if the bitmap is not null, load that instead.
		d = new BitmapDrawable(b);
	}
	
	button.setImageDrawable(d);
}

you can have a fallback drawable to display if something goes wrong (the above bit is Android specific).

A few things to keep in mind:
  1. To get the image out of the zip file you are scanning through all records in the archive sequentially. This could be a performance hit if you have many images. 
  2. If the function returns null but you know that the image is in there, check that you havent accidentally archived the top directory along with the images when creating the zip.

4 comments:

  1. good sample... thanks

    ReplyDelete
  2. Did you hear there's a 12 word sentence you can tell your man... that will induce intense feelings of love and instinctual attractiveness for you deep inside his chest?

    Because deep inside these 12 words is a "secret signal" that triggers a man's impulse to love, cherish and protect you with his entire heart...

    12 Words That Fuel A Man's Love Response

    This impulse is so built-in to a man's brain that it will drive him to try better than ever before to to be the best lover he can be.

    Matter of fact, triggering this dominant impulse is so binding to getting the best ever relationship with your man that the instance you send your man a "Secret Signal"...

    ...You will instantly notice him expose his heart and mind for you in a way he never expressed before and he'll identify you as the only woman in the universe who has ever truly attracted him.

    ReplyDelete