How to get screenshots thumbnails of video files in Android

  • 2021-06-28 13:49:56
  • OfStack

background

Recently, the company asked me to take charge of APP plus video recording and publishing. I simply completed the basic recording and video compression functions. Later, I found that the publishing interface needs to upload video screenshots. I searched the Internet for some information and sorted it out here.

code implementation


/**
*  Get screenshots of video files 
*
* @param path  Path to video file 
* @return Bitmap  Return the acquired Bitmap
*/
public static Bitmap getVideoThumb(String path) {
MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(path);
return media.getFrameAtTime();
}
/**
*  Get thumbnails of video files  API>=8(2.2)
*
* @param path  Path to video file 
* @param kind  Resolution of thumbnails: MINI_KIND , MICRO_KIND , FULL_SCREEN_KIND
* @return Bitmap  Return the acquired Bitmap
*/
public static Bitmap getVideoThumb2(String path, int kind) {
return ThumbnailUtils.createVideoThumbnail(path, kind);
}
public static Bitmap getVideoThumb2(String path) {
return getVideoThumb2(path, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
}

These are the ways to get screenshots and thumbnails of video files. You may also need to save Bitmap as a file:


/**
* Bitmap Save as File
*
* @param bitmap input bitmap
* @param name output file's name
* @return String output file's path
*/
public static String bitmap2File(Bitmap bitmap, String name) {
File f = new File(Environment.getExternalStorageDirectory() + name + ".jpg");
if (f.exists()) f.delete();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
} catch (IOException e) {
return null;
}
return f.getAbsolutePath();
}

The above content is about how to get the screenshots and thumbnails of video files in Android, which is introduced to you by this site. I hope it will be helpful for you!


Related articles: