SoFunction
Updated on 2025-03-11

How to get screenshots and thumbnails of video files in Android

background

The company recently asked me to add video recording and publishing functions to the APP I was responsible for. I simply completed the basic recording and video compression functions. Later, I found that the release interface required uploading screenshots of videos. I searched for information online and sorted them out here.

Code implementation

/**
 * Get screenshot of video file
 *
 * @param path The path of the video file
 * @return Bitmap Returns the obtained Bitmap
 */
public static Bitmap getVideoThumb(String path) {
MediaMetadataRetriever media = new MediaMetadataRetriever();
(path);
return ();
}
/**
 * Get video file thumbnail API>=8(2.2)
 *
 * @param path The path of the video file
 * @param kind Thumbnail resolution: MINI_KIND, MICRO_KIND, FULL_SCREEN_KIND
 * @return Bitmap Returns the obtained Bitmap
 */
public static Bitmap getVideoThumb2(String path, int kind) {
return (path, kind);
}
public static Bitmap getVideoThumb2(String path) {
return getVideoThumb2(path, .FULL_SCREEN_KIND);
}

The above is the method to obtain screenshots and thumbnails of video files. You may also need to save Bitmap into a file:

/**
 * Save Bitmap to 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(() + name + ".jpg");
if (()) ();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
(, 100, fOut);
();
();
} catch (IOException e) {
return null;
}
return ();
}

The above content is the relevant knowledge about how to obtain screenshots and thumbnails of video files in Android introduced to you by the editor. I hope it will be helpful to everyone!