Monday, May 9, 2011

How to create a Thumbnail image in Android

Today, I wanted to create an image thumbnail in Android. There is no easyway out. So I wrote the code by my self.

byte[] imageData = null;

try
{

final int THUMBNAIL_SIZE = 64;

FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();

}
catch(Exception ex) {

}

4 comments:

  1. Aruna can you ever do Encryption decryption in android . I want do encpt decpt a file (PDF,doc,etc) through code ................can you help me

    ReplyDelete
  2. Hi. I have not seen any file encryption algorithms built into Android yet. So I think you have to use your own encryption or you can read the files in bytes and pass it to a algorithm function which does the job for you.

    Also try Java 256bit AES Encryption library external lib which does the encryption for your.

    take a look at http://stackoverflow.com/questions/992019/java-256bit-aes-encryption

    ReplyDelete
  3. close the streams after use? no?

    ReplyDelete
  4. I have an array of images and I want to create thumbnails of all these images. Will this peice of code work for me and Kindly tell me what is the format of "fileName"

    ReplyDelete