3

I want thumbnail from a video at any specific position. I am using ThumbnailUtils in order to get thumbnail from video uri and assigning to bitmap but I am getting null value on bitmap.

Any reasons how this is happening and how do I fix this?

selectedVideoUri = data.getData();

bitmap = ThumbnailUtils.createVideoThumbnail(getRealPathFromURI(videoUri),
                        MediaStore.Images.Thumbnails.MINI_KIND);

public String getRealPathFromURI(Uri contentUri) {
        String res = null;
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
        if(cursor.moveToFirst()){;
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        cursor.close();
        return res;
    }
Debarun Lahiri
  • 142
  • 2
  • 11

4 Answers4

2

You can use Glide to load thumb directly to imageview

Glide.with(activity).load(videoPath).into(imageview);
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
1

First Load Video List with its path in Your array list using below method

 private void loadData(String currentAppPath) {
    hiddenpath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + currentAppPath);
    String[] fileName = hiddenpath.list();
    try{

            for(String f : fileName){
                if(HelpperMethods.isVideo(f)){
                    videoFiles.add(hiddenpath.getAbsolutePath()+"/"+f);
                }
            }
            new Loader().loadImages(Environment.getExternalStorageState());

    }catch (Exception e){

    }
}

You need Loader().loadImages method so i declare this method in separate class file. see below code

public class Loader {
String[] imagieFiles;

public void loadImages(String path){

    Log.e("path",path);
    System.out.println(path);
}  }

Then after You can use below Code to Get Video Thumbnail. By default Each Video Store two size Thumbnail.

1) MINI -- MediaStore.Images.Thumbnails.MINI_KIND and

2) MICRO -- MediaStore.Images.Thumbnails.MICRO_KIND

  Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
                    MediaStore.Images.Thumbnails.MINI_KIND);
            BitmapDrawable bitmapDrawable = new BitmapDrawable(thumb);
            contentViewHolder.videoView.setImageBitmap(thumb);
Nimesh Patel
  • 1,394
  • 13
  • 26
1

This is supported by Android natively using MediaPlayer SeekTo method

If you just want to show the video placeholder to display then you can use below code:

video_view.setVideoPath(videoPath);
video_view.seekTo(3000); // in milliseconds i.e. 3 seconds
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
1

ThumbnailUtils returns null when file or video is corrupted.

but I wanted to only use Uri and this is a good solution to do this:

val mmr = MediaMetadataRetriever()
mmr.setDataSource(videoUri)
val thummbnailBitmap = mmr.frameAtTime
imageView.setImageBitmap(thummbnailBitmap)
DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36