How to get video thumbnail from content uri?

3 weeks ago 25
ARTICLE AD BOX

You’re running into a real Android limitation, not just a bad implementation. Generating thumbnails from content:// video URIs is expensive, and there is no 100% instant, crash-proof API—but there is a correct, efficient, production-ready approach.

Below is the best practice solution used by media apps (Gallery, file managers, WhatsApp-like apps).

Correct & Optimal Solution (Recommended)

✔ Use ThumbnailUtils + MediaStore ID

This gives you system-cached thumbnails, not freshly decoded frames.

Works with content:// URIs, fast, safe, and used internally by Android.

✅ Step 1: Get the MediaStore ID from the content URI

Copy code

Kotlin

fun getVideoId(context: Context, uri: Uri): Long? {

val projection = arrayOf(MediaStore.Video.Media.\_ID) context.contentResolver.query(uri, projection, null, null, null)?.use { cursor -\> if (cursor.moveToFirst()) { return cursor.getLong(0) } } return null

}

✅ Step 2: Generate thumbnail using system cache

Android Q+

Copy code

Kotlin

fun getVideoThumbnail(

context: Context, uri: Uri, size: Size = Size(200, 200)

): Bitmap? {

return try { ThumbnailUtils.createVideoThumbnail( context.contentResolver, uri, size, null ) } catch (e: Exception) { null }

}

✔ Uses cached thumbnails ✔ No MediaMetadataRetriever ✔ No crashes ✔ Fast in lists

Read Entire Article