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())
 }
@Mouadabdelghafouraitali

it's giving me a black color instead of transparent, I've changed JPG to PNG but nothing works

@WrathChaos
Copy link
Author

it's giving me a black color instead of transparent, I've changed JPG to PNG but nothing works

I've updated the Kotling version, can you please try it? @Mouadabdelghafouraitali

@shivayogih
Copy link

I am getting the crash for this

@WrathChaos
Copy link
Author

Hello @shivayogih,
What is the crash?

@KhairuI
Copy link

KhairuI commented Apr 3, 2021

work for me

@anilkumar416
Copy link

giving null not working MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, "Title", null) this is deprecated any other solution for this ?

@phamhoanuit
Copy link

phamhoanuit commented May 21, 2021 via email

@anmol-Git
Copy link

does anyone come out with the solution ??
if yes, please share with all it will be of great help

@Mouadabdelghafouraitali
Copy link

Mouadabdelghafouraitali commented Jul 12, 2021

I ended up using this useful lib : implementation 'com.blankj:utilcodex:1.30.6'

ImageUtils.save(bitmap, cached_path, Bitmap.CompressFormat.JPEG);
Uri bitmap2Uri = UriUtils.file2Uri(FileUtils.getFileByPath(cached_path));

@studysqure
Copy link

cached_path ? how to define it

@MUhammadasadnazar
Copy link

no one had solved the problem?

@Mouadabdelghafouraitali

@MUhammadasadnazar what is the problem that you have?

@MUhammadasadnazar
Copy link

I want to convert bitmap to image_uri but the app is going to crash with error path is null

@Mouadabdelghafouraitali
Copy link

Mouadabdelghafouraitali commented Mar 31, 2022

There's no way to convert a Bitmap to URI, you should store the bitmap as a file in the user device, then use that file to get the URI, once you're finishing with it, remove it, I'm using the following code to achieve that :

First, I save the bitmap as a file :

cachedPath = "CREATE A FOLDER AND ADD (.) BEFORE THE FOLDER NAME TO MAKE IT INVISIABLE TO THE USER"
ImageUtils.save(YOUR_BITMAP, cachedPath, Bitmap.CompressFormat.PNG);

Once the file is saved, you can get the URI like that :

Uri uri = UriUtils.file2Uri(FileUtils.getFileByPath(cachedPath));

I'm using implementation 'com.blankj:utilcodex:1.30.6' to make things easier for me.

@MUhammadasadnazar
Copy link

let me just try thanks

@Catfish-Emperor
Copy link

val path = MediaStore.Images.Media.insertImage([email protected], result ,"Tittle",null)
val uri = Uri.parse(path.toString())

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference

got error in the MediaStore.Images.Media.insertImage([email protected], result ,"Tittle",null) is null. Whats really weird is that it used to work in my exercise app. when I try making another app, it worked for a while then it crash(I didn't do anything that disturb the code). and when I try my old exercise app that used to work in case I changed something in my new app that caused the crash, my old app crash also(I didn't even do anything to my old app) and with the same error

@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