Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Last active July 6, 2023 19:29
Show Gist options
  • Save WrathChaos/68d534fe4feb713770dc41e96dfcaee2 to your computer and use it in GitHub Desktop.
Save WrathChaos/68d534fe4feb713770dc41e96dfcaee2 to your computer and use it in GitHub Desktop.
Android: How to convert Bitmap to Uri?
fun getImageUriFromBitmap(context: Context, bitmap: Bitmap): Uri{
    val bytes = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
    val path = MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, "Title", null)
    return Uri.parse(path.toString())
 }
@OhadAms
Copy link

OhadAms commented Jan 30, 2023

There is a problem with the storage when using that implementation - it works fine until you have 32 (2^5) pictures taken from the app saved in your gallery, that's when it crashes.
if you delete one photo you taken in the app from the gallery you can take another one in the app before it crashes again - until you get to 32 again.
Don't know the reason it happens though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment