使用Coil在Jetpack Compose中从手机本地存储显示视频的缩略图。

huangapple go评论71阅读模式
英文:

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&lt;Any&gt; { data, options, imageLoader -&gt;
                        imageLoader.components.newBuilder()
                            .add(VideoFrameDecoder.Factory())
                            .build()
                            .newFetcher(data, options, imageLoader)?.first
                    }
                    .videoFrameMillis(1000)
                    .build(),
            )
            Image(
                painter = painter,
                contentDescription = &quot;&quot;,
                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 = &quot;&quot;,
    contentScale = ContentScale.Crop,
    alignment = Alignment.Center,
    modifier = Modifier.size(48.dp)
)

huangapple
  • 本文由 发表于 2023年3月3日 19:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定