Hi I am beginner in Android Development, I am trying to learn Jetpack Compose but I'm facing one problem which I can't resolve. I have this code for opening gallery and selecting image
@Composable
fun LoadImage(
bitmap: MutableState<Bitmap?>
) {
var imageUri by remember {
mutableStateOf<Uri?>(null)
}
val context = LocalContext.current
val launcher = rememberLauncherForActivityResult(
contract =
ActivityResultContracts.GetContent()
) { uri: Uri? ->
imageUri = uri
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxWidth()
.padding(6.dp)
) {
Button(onClick = {
launcher.launch("image/*")
}) {
Text(text = "Izaberi sliku")
}
}
Log.d("DEBUG", "IMAGE URI $imageUri")
imageUri?.let {
if (Build.VERSION.SDK_INT < 28) {
bitmap.value = MediaStore.Images
.Media.getBitmap(context.contentResolver, it)
} else {
val source = ImageDecoder
.createSource(context.contentResolver, it)
bitmap.value = ImageDecoder.decodeBitmap(source)
}
}
}
I call it from this component:
@Composable
fun ShowImage(
member: Member,
snackScope: CoroutineScope,
snackHostState: SnackbarHostState,
) {
val bitmap = remember {
mutableStateOf<Bitmap?>(null)
}
if (bitmap.value != null) {
Image(
painter = rememberAsyncImagePainter(bitmap.value),
contentDescription = null,
modifier = Modifier.size(400.dp)
)
}
Row(
modifier = Modifier
.padding(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
LoadImage(bitmap = bitmap)
}
...
What happens: it works perfectly in Pixel 2 API 28 Emulator, but when I run it in LG K22 (Android 10) it does not show the image when it is picked from Gallery. Acrually, if I select image from e.g. /Pictures/Viber folder it is shown, but if I select from any other folder (e.g. Download, DCIM) it does not show, it is just white space, also when I upload that image to Firebase Storage it is all black. Can someone explain what happens and what am I doing wrong? Actually I tried uploading same image which I found in Images/Viber folder from Download directory (copied it, then loaded from application) and it loads. The only difference that I see is that image which loads correctly is about 200KB but image which is not loading correctly is about 3MB. Is there any way to load and show image from gallery in Jetpack Compose? I googled a lot but haven't found any solution except this one.