rajesh
09/09/2021, 5:42 PMLazyVerticalGrid
to loading images. If there's any error while decoding image bitmap, it shouldn't be loaded (as per code in thread), but LazyVerticalGrid
is using empty cell (shown in image) instead of avoiding that item. what should i do to resolve it?rajesh
09/09/2021, 5:43 PM@Composable
fun ImageGridView(
context: Context,
images: List<ImageMetaData>
) {
LazyVerticalGrid(cells = GridCells.Fixed(3)) {
items(images) { image ->
var bitmap: Bitmap? = null
try {
bitmap = context.contentResolver.loadThumbnail(image.uri, Size(640, 480), null)
} catch (e: IOException) {
Log.d(TAG, "IO exception: ${e.message}")
}
bitmap?.let {
Image(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
.border(1.dp, Color.White),
bitmap = it.asImageBitmap(),
contentDescription = null,
contentScale = ContentScale.Crop
)
}
}
}
}
rajesh
09/09/2021, 5:43 PMAlexandre Elias [G]
09/09/2021, 8:52 PMreportLoadError(imageIdentifierKey: String)
that you would call in the catch block. then your higher-level code should mutate the list to remove that image, causing your whole ImageGridView
to recomposeAdam Powell
09/09/2021, 8:59 PMrajesh
09/09/2021, 10:13 PMrajesh
09/09/2021, 10:14 PMLaunchedEffect
but it says inappropriate method blocking call for loadThumbnail()
and do nothing.Alexandre Elias [G]
09/09/2021, 10:22 PMLazyListState
scroll position will not be affected as long as the item with an error is not above the top of the screen (and even then a manual adjustment to the state should be possible)Adam Powell
09/10/2021, 12:28 AMrajesh
09/10/2021, 5:11 PMrajesh
09/10/2021, 5:11 PM@ExperimentalFoundationApi
@Composable
fun ImageGridView(
modifier: Modifier = Modifier,
context: Context,
images: List<ImageMetaData>,
onClick: (ImageMetaData) -> Unit
) {
val scope = rememberCoroutineScope()
LazyVerticalGrid(
modifier = modifier,
cells = GridCells.Fixed(4)
) {
items(images) { image ->
var bitmap: Bitmap? = null
scope.launch {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
try {
bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
context.contentResolver.loadThumbnail(image.uri, Size(640, 480), null)
} else {
MediaStore.Images.Thumbnails.getThumbnail(
context.contentResolver,
image.id,
MediaStore.Images.Thumbnails.MINI_KIND,
null
)
}
} catch (e: IOException) {
}
}
}
bitmap?.let {
Image(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
.border(1.dp, Color.White)
.clickable { onClick(image) },
bitmap = it.asImageBitmap(),
contentDescription = null,
contentScale = ContentScale.Crop
)
}
}
}
}