英文:
How to Display video thumbnail for videos from local storage of Phone in jetpack compose using Coil
问题
如何在Jetpack Compose中使用Coil显示来自手机本地存储的视频的视频缩略图
我正在尝试以下代码
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data(videouri)
.fetcherFactory<Any> { data, options, imageLoader ->
imageLoader.components.newBuilder()
.add(VideoFrameDecoder.Factory())
.build()
.newFetcher(data, options, imageLoader)?.first
}
.videoFrameMillis(1000)
.build(),
)
Image(
painter = painter,
contentDescription = "",
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
modifier = Modifier.size(48.dp)
)
视频的URI是
ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, idColumn.toLong())
但是视频缩略图没有显示。
英文:
How to Display video thumbnail for videos from local storage of Phone in jetpack compose using Coil
I am trying this code
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data(videouri)
.fetcherFactory<Any> { data, options, imageLoader ->
imageLoader.components.newBuilder()
.add(VideoFrameDecoder.Factory())
.build()
.newFetcher(data, options, imageLoader)?.first
}
.videoFrameMillis(1000)
.build(),
)
Image(
painter = painter,
contentDescription = "",
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
modifier = Modifier.size(48.dp)
)
The video uri is
ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, idColumn.toLong())
However videothumbnail doesnot show.
答案1
得分: 1
不确定你是否已经找到了解决方案。但最好的方法是使用 MediaMetadataRetriever
获取缩略图,然后使用 Coil 显示 Bitmap
。
首先,从 MediaStore
URI 生成缩略图:
val mediaMetadataRetriever = MediaMetadataRetriever()
var thumbnail: Bitmap? = null
try {
mediaMetadataRetriever.setDataSource(context, uri)
thumbnail = mediaMetadataRetriever.getFrameAtTime(2000000) // 时间单位为微秒
} catch (e: Exception) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
}
} finally {
mediaMetadataRetriever.release()
}
然后使用 Coil 进行显示:
AsyncImage(
model = thumbnail,
contentDescription = "",
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
modifier = Modifier.size(48.dp)
)
英文:
Not sure if you've found a solution already. But best would be to use MediaMetadataRetriever
to retrieve your thumbnail and then display the Bitmap
using Coil.
First, generate thumbnail from MediaStore
URI:
val mediaMetadataRetriever = MediaMetadataRetriever()
var thumbnail: Bitmap? = null
try {
mediaMetadataRetriever.setDataSource(context, uri)
thumbnail = mediaMetadataRetriever.getFrameAtTime(2000000) // time in Micros
} catch (e: Exception) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
}
} finally {
mediaMetadataRetriever.release()
}
And then display using Coil:
AsyncImage(
model = thumbnail,
contentDescription = "",
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
modifier = Modifier.size(48.dp)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论