Hi there, is there a way to convert Bitmap into ImageBitmap ? I would like to use the Image composab...
m
Hi there, is there a way to convert Bitmap into ImageBitmap ? I would like to use the Image composable to display images that are stored locally. My idea was to use
BitmapFactory.decodeFile(MY_FILE_PATH)
to convert the images into Bitmap first and then from Bitmap to ImageBitmap
n
The way to handle it now is that you can use the Glide or Coil libraries to load them
Copy code
CoilImage(Uri.fromFile(path...), null)
m
Amazing, thanks so much 🙂
a
if you already have an android
Bitmap
you can use the
.asImageBitmap()
extension: https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/package-summary#asimagebitmap
m
Copy code
BitmapFactory.decodeByteArray(localByteArray, 0, locakByteArray.size).asImageBitmap()
and yo don't need anything else
a
but the reason to use something like coil or glide is to handle cache scoping; reloading and re-decoding the same bitmaps over and over will tank performance
n
ufff i see
m
that's why you put them in a hassmap
a
and then design a cache eviction policy, etc. 🙂
you can absolutely do it yourself, but tuning it is more work than it looks like at first.
m
@Adam Powell so you're not recommending the use of Coil or Glide ?
a
I am recommending using them for their cache management. I am not recommending using them if you already have that problem space handled in some other way.
c
If you just want to convert Bitmap to ImageBitmap, use
.asImageBitmap()
, which is very easy. If you want to load a Bitmap and use it with Coil's or Gilde's many out-of-the-box features, you can use
accompanist
, otherwise you can simply use
.asImageBitmap()
, and apply it to an Image.
1157 Views