0

I have a Bitmap image that I want to save and have it show up in my gallery, unfortunately I am having a lot of issues trying to complete this task. Since yesterday I've probably tried a dozen different code snippets to do just this. I think what's happening is that these code snippets are working, but they're saving it in the application's data and not in the shared storage/gallery.

How do I re-adapt this code to have it store images in a folder that any gallery application or file manager can access and not just in the application's data?

private fun saveToGallery(bitmap: Bitmap) {

        Toast.makeText(applicationContext,"Saving image!", Toast.LENGTH_SHORT).show()
        var outputStream: FileOutputStream? = null
        val file = Environment.getExternalStorageDirectory()
        val dir = File(file.absolutePath.toString() + "/MyPics")
        dir.mkdirs()
        val filename = String.format("%d.png", System.currentTimeMillis())
        val outFile = File(dir, filename)
        try {
            outputStream = FileOutputStream(outFile)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        try {
            outputStream!!.flush()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        try {
            outputStream!!.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

1 Answers1

0

You should send a broadcast event after saving the image.

For example: https://stackoverflow.com/a/39448816/5313283

Mücahid Kambur
  • 1,610
  • 13
  • 10