0

I save a png image to external storage using this block of code for sdk<=28

/**
 * save image with this method if the sdk is 28 or lower
 */
private fun saveImageSdk28(fileName: String){
    //declar the output stream variable outside of try/catch so that it can always be closed
    var imageOutputStream: FileOutputStream? = null

    var outputImageFile = getFile(fileName)
    if (!outputImageFile.exists()) {
        outputImageFile.createNewFile()
    }

    try {
        imageOutputStream = FileOutputStream(outputImageFile)
        encryptedBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageOutputStream)
    } catch (e: IOException) {
        e.printStackTrace()
        Timber.i(e.toString())
    } finally {
        if (imageOutputStream != null) {
            imageOutputStream.flush()
            imageOutputStream.close()
        }
    }
}
/**
 * returns file from fileName
 */
fun getFile(fileName: String): File{
    //open, or create the directory where the image will be stored
    var directory = File(
        Environment.getExternalStorageDirectory().toString() + "/AppNameOutput/"
    )
    if (!directory.exists()) {
        directory.mkdir()
    }
    //create the file
    var file: File = File(directory.absolutePath, fileName)
    return file
}

and this code for when the sdk>28

/**
 * save image with this method if the sdk is 29 or higher
 */
@RequiresApi(Build.VERSION_CODES.Q)
private fun saveImageSdk29(fileName: String){
    val imageCollection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
    val contentValues = ContentValues().apply {
        put(MediaStore.Images.Media.DISPLAY_NAME, "$fileName")
        put(MediaStore.Images.Media.MIME_TYPE, "image/png")
        put(MediaStore.Images.Media.WIDTH, encryptedBitmap.width)
        put(MediaStore.Images.Media.HEIGHT, encryptedBitmap.height)
    }

    try{
        val contentResolver = getApplication<Application>().contentResolver

        contentResolver.insert(imageCollection, contentValues)?.also {uri->
            contentResolver.openOutputStream(uri).use {outputStream ->
                encryptedBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
            }
        }
    }catch (e: IOException){
        e.printStackTrace()
    }
}

The image sucsessfully saves on the users device and can be accesed through files, however, the user can't access these images through the gallery, or Images tab.

  • `if (!directory.exists()) { directory.mkdir() }` That should be: `if (!directory.exists()) { if(!directory.mkdir()) return }`. Display a Toast too to inform the user when this happens. – blackapps Nov 29 '22 at 20:14
  • What do you consider to be 'the gallery'? And where is that Images tab visible? – blackapps Nov 29 '22 at 20:18
  • 1
    For the <=28 code you should add some code lines to let your file be scanned/indexed by the media store. – blackapps Nov 29 '22 at 20:19
  • 1
    For >28 you let the media store create the file. It should be directly known then by the media store. – blackapps Nov 29 '22 at 20:20
  • This question has been already answered https://stackoverflow.com/a/22951564/20538598 – codeX Nov 30 '22 at 13:23

1 Answers1

-3

I solved it. Turns out you just need to wait a while and reboot the phone for the gallery to show your images.