Skip to main content

How to save an image displayed in ImageView to the device's gallery in Kotlin Android

How to save an image displayed in ImageView to the device's gallery in Kotlin Android.

Here's a detailed step-by-step tutorial on how to save an image displayed in ImageView to the device's gallery in Kotlin Android:

Step 1: Add the necessary permissions to the AndroidManifest.xml file In order to save an image to the device's gallery, you need to add the WRITE_EXTERNAL_STORAGE permission to your app's AndroidManifest.xml file. Open the file and add the following line within the <manifest> tag:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 2: Create a function to save the image

Next, create a function that will handle the saving of the image. This function will take the ImageView as a parameter and will perform the necessary steps to save the image to the device's gallery. Here's an example of how this function can be implemented:

private fun saveImageToGallery(imageView: ImageView) {
// Get the drawable from the ImageView
val drawable = imageView.drawable

// Create a bitmap from the drawable
val bitmap = (drawable as BitmapDrawable).bitmap

// Get the path to the device's gallery
val galleryPath =
Environment.getExternalStorageDirectory().toString() + File.separator + Environment.DIRECTORY_DCIM + File.separator + "Camera"

// Create a file to save the image
val file = File(galleryPath, "image.jpg")

try {
// Create an output stream to write the bitmap to the file
val outputStream = FileOutputStream(file)

// Compress the bitmap and write it to the output stream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)

// Flush and close the output stream
outputStream.flush()
outputStream.close()

// Notify the media scanner about the new image
MediaScannerConnection.scanFile(
applicationContext,
arrayOf(file.toString()),
null,
null
)
} catch (e: IOException) {
e.printStackTrace()
}
}

Step 3: Call the saveImageToGallery function

Now that we have the function to save the image, we need to call it at the appropriate time. This will depend on your app's requirements. For example, you might want to save the image when the user clicks a button. Here's an example of how you can call the saveImageToGallery function when a button is clicked:

val saveButton: Button = findViewById(R.id.save_button)
val imageView: ImageView = findViewById(R.id.image_view)

saveButton.setOnClickListener {
saveImageToGallery(imageView)
}

In this example, saveButton is the button that triggers the save action and imageView is the ImageView that displays the image you want to save.

Step 4: Request the WRITE_EXTERNAL_STORAGE permission at runtime

Starting from Android 6.0 (Marshmallow), you need to request the WRITE_EXTERNAL_STORAGE permission at runtime. Here's an example of how you can do this:

val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
val requestCode = 123

if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
} else {
// Permission already granted, call the saveImageToGallery function here
saveImageToGallery(imageView)
}

In this example, permission is the WRITE_EXTERNAL_STORAGE permission, requestCode is a unique code to identify the permission request, and imageView is the ImageView that displays the image you want to save.

Step 5: Handle the permission request result

Finally, you need to handle the permission request result in the onRequestPermissionsResult function. Here's an example of how you can do this:

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
if (requestCode == 123) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, call the saveImageToGallery function here
saveImageToGallery(imageView)
} else {
// Permission denied, handle the error accordingly
}
}
}

In this example, requestCode is the unique code that you used when requesting the permission, imageView is the ImageView that displays the image you want to save, and you can handle the permission result accordingly.

And that's it! You now have a step-by-step guide on how to save an image displayed in ImageView to the device's gallery in Kotlin Android.