I'm following <https://medium.com/@dheerubhadoria/...
# compose-android
a
I'm following https://medium.com/@dheerubhadoria/capturing-images-from-camera-in-android-with-jetpack-compose-a-step-by-step-guide-64cd7f52e5de and I'm getting good results in creating the file and getting a Uri for the file. However, I have a problem with showing the photo taken by camera using the file Uri with
rememberAsyncImagePainter
(code in thread)
solved 1
Basically, with the file Uri I'm doing:
Copy code
Image(
                        painter = rememberAsyncImagePainter(
                            model = ImageRequest.Builder(context)
                                .data(photoUri)
                                .crossfade(true)
                                .build()
                        ),
                        contentDescription = null
                    )
however, it only works once (when the permissions are requested for the camera for taking a photo) - the photo will display correctly; however, afterwards if I take another picture, the photo will not be displayed. Anyone ran into this issue?
i
There's quite a few things wrong with that code IMO - it is calling
createImageFile
as part of composition and not even remembering it, so any recomposition in that scope (or process death or rotation while the camera is up) is going to wipe out the Uri and cause you to lose your image
There was another thread just recently where we talked about using
rememberSaveable
on your generated Uri to cache it even over process death (which is very likely on many devices when launching their camera app): https://kotlinlang.slack.com/archives/C04TPPEQKEJ/p1723488484235079?thread_ts=1723488484.235079&amp;cid=C04TPPEQKEJ
But your problem is something slightly different, but related: if you reuse the same Uri more than once, any decent image library is just going to reuse the cached image it already loaded, rather than fetch it again
So your best bet is to create the Uri as part of your button click (e.g., right before your
launch()
) that ensures that each invocation uses a unique Uri, store that in a variable that uses
rememberSaveable
storing that Uri so that it is accessible when you come back
thank you color 1