0

I want to show picture with Coil from Uri.

Following code shows picture but when I choose one. I want to store Uri and when Screen starts i want to show it immediately.

 val uri = "content://com.android.providers.media.documents/document/image%3A18".toUri()
    var imageUri by remember { mutableStateOf<Uri?>(null) }
    val context = LocalContext.current

    val launcher =
        rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) { 
           uri: Uri? ->
            imageUri = uri
        }
    Column {

        Button(onClick = {
            //here we are going to add logic for picking image
            launcher.launch(
                "image/*"
            )

        }, content = {
            Text(text = "Select Image From Gallery")
        })
AsyncImage(
            model = ImageRequest.Builder(context = context)
                .data(imageUri)
                .crossfade(true)
                .build(),
            contentDescription = "",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(144.dp)
        )

        Image(
            painter = rememberAsyncImagePainter(imageUri),
            contentDescription = "Picture",
        )

val uri is uri I chooshed before and I copied it and put to string to show image. But this way first I need to choose picture and it will show. If I put uri instead of imageUri in .data() or rememberAsyncImagePainter nothing happens.

Equlo
  • 115
  • 10
  • "I want to store Uri" -- you need to [take persistable permission](https://commonsware.com/blog/2020/08/08/uri-access-lifetime-still-shorter-than-you-might-think.html) for that. – CommonsWare Jan 25 '23 at 18:59
  • With just uri I cannot "read" image from system ? – Equlo Jan 25 '23 at 19:20

0 Answers0