How to load local video cover in Android

How to load local video cover in Android

There are many ways to load the cover (usually called thumbnail or preview) of local videos in Android. Here are some common methods:

Using MediaMetadataRetriever

MediaMetadataRetriever is a class provided by Android that can be used to retrieve metadata for media files (such as video and audio). You can use it to extract the cover of a video.

 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource("/sdcard/Download/video.mp4"); // 设置视频文件路径Bitmap bitmap = retriever.getFrameAtTime(100000, MediaMetadataRetriever.OPTION_CLOSEST); // 获取封面,这里使用100000微秒(即0.1秒)作为时间戳// 接下来,你可以使用这个Bitmap作为ImageView的源,或者保存到文件中

Using MediaStore Query

This code queries the thumbnail path for the specified video, which can be used to display the video cover as needed.

 Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Video.Media.ALBUM + "=?", new String[]{Constants.DIRECTORY_VIDEO}, MediaStore.Video.Media.DEFAULT_SORT_ORDER); try { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)); //视频缩略图路径String albumPath = null; Cursor thumbCursor = context.getApplicationContext().getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,null, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null); if (thumbCursor.moveToFirst()) { albumPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)); Bitmap bitmap = BitmapFactory.decodeFile(albumPath); } } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) cursor.close(); }

Using FFmpeg

FFmpeg is a powerful open source multimedia processing library that can be used to process a variety of media formats such as video and audio. You can use FFmpeg to extract the cover of a video. However, this usually requires you to write some JNI code to call FFmpeg's native library, or use some Android libraries that encapsulate FFmpeg functions.

(1) You need to integrate the FFmpeg library into your Android project. You can use a ready-made third-party library, https://github.com/WritingMinds/ffmpeg-android-java, or compile the FFmpeg library yourself and integrate it into your project.

(2) Use FFmpeg's command line function to obtain the video cover.

 ffmpeg -i /sdcard/Download/video.mp4 -vframes 1 -vf "scale=640:480" /sdcard/Download/output.jpg

The command will extract the first frame from the video.mp4 video, scale it to 640x480 pixels, and save it as output.jpg.

(3) In the Android application, you can use Java code to execute FFmpeg commands. Use ProcessBuilder or similar methods to execute commands and process the output results of the commands.

 execute(String cmd, FFmpegExecuteResponseHandler ffmpegExecuteResponseHandler) throws FFmpegCommandAlreadyRunningException

Using third-party libraries

Some third-party libraries provide a simpler interface to extract video covers. For example, the Glide image loading library may provide such a function.

 Glide.with(context) .load(Uri.fromFile(new File(filePath))) .into(mImageView);

Precautions

  • Make sure you have permission to access and process the target video file.
  • Processing video files can take some time, especially when dealing with large files or on low-performance devices. You need to perform these operations in a background thread to avoid blocking the UI thread.
  • The quality of the extracted cover may vary depending on video encoding, resolution, etc. The code needs to be tweaked to get the best results.

<<:  iOS 18 starts internal testing, what does the interface look like?

>>:  Understand the Activity startup process and efficient collaboration from startActivity to ATMS

Recommend

Why is cancer discovered in the late stage?

Speaking of cancer, what I remember most vividly ...

5000 words to analyze the philosophy of planting grass when browsing Taobao!

It’s Double Eleven again this year. At 0:00 on No...

Chengdu high-end tea tasting girl with the best quality 2022

The best quality place for high-end tea tasting g...

When will the 2020 global epidemic end? How long will it last?

The new coronavirus is currently breaking out acr...

How to create a popular internet celebrity brand from 0 to 1?

Since last year, consumption has once again becom...

Community Operation SOP Strategy

SOP generally refers to standard operating proced...

Can an annual physical examination really help you “detect cancer early”?

Most people regard routine annual physical examin...

How to break the offline defeat of new consumer brands?

In the past two years, some consumer brands targe...

How long can a shit be? It's twice the length of your rectum...

Expert of this article: Yang Chao, PhD in Chemist...

Liulishuo-In-depth analysis of product operations!

In an era of increasingly fierce competition, adu...