简体   繁体   中英

Get image uri and display it with Coil in Compose (Android)

I'm trying to fetch image uri and then display it in my application with coil.

var imageUri by remember { mutableStateOf<Uri?>(null) }
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")
        })
        Log.d(TAG, "ClientCardItem: $imageUri")
        Text(text = imageUri.toString())

I made this just to check if i fetched uri. I got result like this: "content://com.android.providers.media.documents/document/image%3A18", i put it to string.

This is for image display:

Image(
    painter = rememberImagePainter(
        data  = Uri.parse(uri)  // or ht
    )
    ,
    contentDescription = "123",
    modifier = Modifier.height(200.dp).width(200.dp),
    contentScale = ContentScale.FillWidth
)

I know that I dont have.jpg or.png or something else to display it properly. My question is how to get that format to display picture I picked.

EDIT: rememberAsyncImagePainter, rememberImagePainter or AsyncImage are not found?

After I updated to:

implementation "io.coil-kt:coil:2.2.2"

On AsyncImage() -> Unresolved reference: AsyncImage

To display the image just use:

AsyncImage(model = imageUri, contentDescription = "Picture")

or:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM