0

I want a button to be clicked that saves an image view to the android devices gallery in kotlin. I have this

val image = findViewById<ImageView>(R.id.QRImage)
AK105
  • 39
  • 5
  • `val image = ... ` That should be `val imageView = ...`. And you cannot store imageviews to the gallery but only imsges. – blackapps Jan 23 '22 at 04:33

2 Answers2

0

You can convert your image to bitmap first like the code below:

val image = findViewById<ImageView>(R.id.QRImage)
val imageBitmap = image.drawable.toBitmap()

Then save the bitmap where you want like the code below:

val fileOutputStream = FileOutputStream("Location") //location of the image 
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)

or save it directly like the code below:

 MediaStore.Images.Media.insertImage(context.contentResolver, imageBitmap, "Image title ", null)
0

I think, this should work: MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

Also, it is written in this question: android - save image into gallery

G3nz0
  • 3
  • 2
  • Insert image is deprecated in java:70 am I still able to use it without issue? – AK105 Jan 23 '22 at 20:40
  • @AnshKarnwal - yes, it won't break anything, it's just an outdated way. but the documentation suggests using ContentValues ​​and MediaStore.isPending(). check this: https://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#IS_PENDING and check this:https://stackoverflow.com/questions/57726896/mediastore-images-media-insertimage-deprecated – G3nz0 Jan 23 '22 at 21:09