I’m trying to extract a bitmap from a video that isn’t on the device, suchas a cloud media app like Google Photos. I have no issues when the video was recorded with the actual device, but if the video is synced to google photos from another device I just get back null
.
@Composable
fun VideoThumbnail(uri: Uri) {
val context = LocalContext.current
var bitmap by remember { mutableStateOf<Bitmap?>(null) }
LaunchedEffect(uri) {
bitmap = getBitmapFromUri(context, uri)
}
bitmap?.let {
Image(
modifier = Modifier
.clip(RoundedCornerShape(10.dp))
.fillMaxWidth(),
bitmap = it.asImageBitmap(),
contentDescription = "Video Thumbnail",
)
}
}
fun getBitmapFromUri(context: Context, uri: Uri): Bitmap? {
val contentResolver: ContentResolver = context.contentResolver
var bitmap: Bitmap? = null
try {
val inputStream: InputStream? = contentResolver.openInputStream(uri)
bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
} catch (e: Exception) {
e.printStackTrace()
}
return bitmap
}
2