Android: How do I get thumbnail from a Video (Uri)

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;
    }

3 answers

  • answered 2019-08-13 03:52 CodeInsideCofee

    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);
    

  • answered 2019-08-13 04:37 Silversky Technology

    You can use Glide to load thumb directly to imageview

    Glide.with(activity).load(videoPath).into(imageview);
    

  • answered 2019-08-13 04:43 Rahul Khurana

    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