Create Thumbnails in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Create Thumbnails in Android

Postby vivekkalady » Sun Nov 30, 2014 1:16 pm

First Method

Thumbnails can be created by using ThumbnamilUtils class available in android

Image Thumbnail

Code: Select all

final int THUMBSIZE = 64;

Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);



Video Thumbnail

Code: Select all

 
 // MICRO_KIND: 96 x 96 thumbnail
bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,Thumbnails.MICRO_KIND);
       
// MINI_KIND: 512 x 384 thumbnail
bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,Thumbnails.MINI_KIND);


Second Method

This function maintains aspect ratio.it decodes the bitmap and create a thumbnail.

Code: Select all

public static final int THUMBNAIL_HEIGHT = 48;
public static final int THUMBNAIL_WIDTH = 66;

imageBitmap = BitmapFactory.decodeByteArray(mImageData, 0, mImageData.length);
Float width  = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false);

int padding = (THUMBNAIL_WIDTH - imageBitmap.getWidth())/2;
imageView.setPadding(padding, 0, padding, 0);
imageView.setImageBitmap(imageBitmap);

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron