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())
}
-
-
Save WrathChaos/68d534fe4feb713770dc41e96dfcaee2 to your computer and use it in GitHub Desktop.
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
I am getting the crash for this
Hello @shivayogih,
What is the crash?
work for me
giving null not working MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, "Title", null) this is deprecated any other solution for this ?
does anyone come out with the solution ??
if yes, please share with all it will be of great help
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));
cached_path ? how to define it
no one had solved the problem?
@MUhammadasadnazar what is the problem that you have?
I want to convert bitmap to image_uri but the app is going to crash with error path is null
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.
let me just try thanks
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
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.
it's giving me a black color instead of transparent, I've changed JPG to PNG but nothing works