Android How to Create Runtime Thumbnail

Android how to create runtime thumbnail

My Solution

byte[] imageData = null;

try
{

final int THUMBNAIL_SIZE = 64;

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

imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

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

}
catch(Exception ex) {

}

Create dynamic thumbnail image from drawable resource and set to imageView

You need to change two things here :

1) BitmapFactory.decodeFile(categoryImagesList[position]), 100, 100) to BitmapFactory.decodeResource(getResources(),categoryImagesList[position]);

2) Change holder.iv.setImageResource(thumbImage); to holder.iv.setImageBitmap(thumbImage);

Android: Is it possible to display video thumbnails?

If you are using API 2.0 or newer this will work.

int id = **"The Video's ID"**
ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);

how can I show a video thumbnail from a video path?

It is the default way to create a thumbnail.

For Mini Kind

Bitmap thumb;
//MINI_KIND, size: 512 x 384 thumbnail
thumb = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
img_tumbnail.setImageBitmap(thumb);

For Micro Kind

Bitmap thumb;
//MICRO_KIND, size: 96 x 96 thumbnail
thumb= ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MICRO_KIND);
img_tumbnail.setImageBitmap(thumb);

Also, you can use Glide for Url as well as Video path of Device.

Glide.with(context).with(this)
.asBitmap()
.load(videoFilePath) // or URI/path
.into(imgView); //imageview to set thumbnail to

also, you can resize thumbnail by using .override(50,50) with Glide.

Android createVideoThumbnail returning null, but throwing Runtime Exception internally

You can use Glide to retrieve Thumbnail of Video.

// 1st: Generate image and set to imageview

Glide.with(context).asBitmap()
.load(filePathWithExtension)
.into(imageview);

// 2nd: Get Bitmap from Glide

GlideApp.with(context)
.asBitmap()
.load(filePathWithExtension)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
// You can use **resource**.
imageview.setImageBitmap(resource);
}
});


Related Topics



Leave a reply



Submit