6

I want some help in creating thumbnail of a video being recorded from my android phone and I got this code from this Link, and I modified this part of the code to generate the thumbnail but somehow the thumbnail is not being generated, so any help will be appreciated.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) {
        videoFileUri = data.getData();
        if (videoFileUri != null) {
           Bitmap image= ThumbnailUtils.createVideoThumbnail(videoFileUri.toString(),MODE_APPEND);

 ImageView imageView=(ImageView)findViewById(R.id.image);
 imageView.setImageBitmap(image);
        }

        playVideoButton.setEnabled(true);
    }
}
ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
Vipul Singh
  • 163
  • 1
  • 3
  • 13
  • It seems like you're creating the thumbnail Bitmap (I assume) and then do nothing with it. Is this really all the relevant code? – SpaceBison Jun 20 '18 at 05:55
  • i have to set that bitmap inside an imageview, now i have edited my answer pls have a look at it – Vipul Singh Jun 20 '18 at 06:07
  • @VipulSingh please try to load bitmap using Picasso or Glide i have check its working all the time.[link](https://stackoverflow.com/a/50941805/5077421) _italic_ **bold** – Jaydip Radadiya Jun 20 '18 at 06:44

4 Answers4

21

You can use glide. its automatically set thumb image of video.

Glide is also able to display the thumbnail of videos, as long as they're stored on the phone. Let's assume you get the file path by letting the user select a video: Based on this document https://futurestud.io/tutorials/glide-displaying-gifs-and-videos

String filePath = "/storage/emulated/0/Pictures/example_video.mp4";

Glide  
    .with(context)
    .asBitmap()
    .load(Uri.fromFile(new File(filePath)))
    .into(imageViewGifAsBitmap);
Mayur Patel
  • 2,300
  • 11
  • 30
0

Here is the complete tested working solution:

Try it.

    if (videoFileUri != null) {

                        Bitmap bitmapThumb = null;
                        try {
                            bitmapThumb = ThumbnailUtils.createVideoThumbnail(videoFileUri.toString(),
                                    MediaStore.Video.Thumbnails.MINI_KIND);// MINI_KIND, size: 512 x 384 thumbnail | MICRO_KIND, size: 96 x 96 thumbnail

                            ImageView imageView = (ImageView) findViewById(R.id.image);
//                            imageView.setImageBitmap(bitmapThumb);

                            Uri uri = getImageUri(context, bitmapThumb);

                            Picasso.with(context)
                                    .load(uri)
                                    .placeholder(R.drawable.ic_imagedefault)
                                    .error(R.drawable.ic_imagedefault)
                                    .resize(350, 200)//as per need.
                                    .centerCrop()
                                    .into(imageView);

                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            bitmapThumb = null;
                        }
                    }
                }

Here is the static method for getImageUri :

public static Uri getImageUri(Context inContext, Bitmap inImage) {

    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    } catch (Exception e) {
        e.printStackTrace();
        return Uri.parse("");
    }

}
0
 Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MICRO_KIND);
        

This works fine for me. https://developer.android.com/reference/android/media/ThumbnailUtils

Ismail Osunlana
  • 434
  • 5
  • 9
-4
Uri uri = getImageUri(context, bitmapThumb);
Glide.with(getContext()).
load(uri).
thumbnail(0.1f).
into(imageView);
wscourge
  • 10,657
  • 14
  • 59
  • 80