Search code examples
androidimageandroid-studiodelete-fileandroid-storage

Delete a image from InternalStorage using ImagePath


I am working with Android version 10. I have enabled Permissions to Read & Write Storage Device Name : Poco F1

Scenario: I have to capture a screenshot of the current layout and save it to internalStorage and preview that image to the user. Here users have an option to delete the image.

Here are the codes I am using to save & delete

Saving a screenshot:

//I will pass the bitmap here
fun saveBitmapToInternalStorage(bitmap: Bitmap?) {
    bitmap?.let {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            saveBitmapToOlderDevice(it)
        } else {

            saveBitmapToNewerDevice(it)
        }
    }
}





//This method is to save image to newerdevice >= Q
@RequiresApi(Build.VERSION_CODES.Q)
private fun saveBitmapToNewerDevice(bitmap: Bitmap) {
    val uri = generateUri()
    context.contentResolver.openOutputStream(uri ?: return).use { outputStream ->
        outputStream?.let {
            writeBitmapToJpeg(bitmap, outputStream, uri.toString())
        }
    }
}





//This is to generate the URI.
@RequiresApi(Build.VERSION_CODES.Q)
private fun generateUri(): Uri? {
    val dateFormat = getDateFormat()
    val contentValues = ContentValues().apply {
        put(MediaStore.MediaColumns.DISPLAY_NAME, "${dateFormat}.jpg")
        put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
        put(MediaStore.MediaColumns.RELATIVE_PATH, "Pictures/${context.resources.getString(R.string.app_name)}")
    }
    return context.contentResolver.insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        contentValues
    )
}





// To save images to olderDevice
private fun saveBitmapToOlderDevice(bmp: Bitmap) {
    val filename = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)?.absolutePath +
            "/${context.resources.getString(R.string.app_name)}/${getDateFormat()}.jpg"
    createDirectory(filename)
    val outputStream = FileOutputStream(filename)

    writeBitmapToJpeg(bmp, outputStream, filename)
}





//This method is to save the image to InternalStorage
private fun writeBitmapToJpeg(bmp: Bitmap, outputStream: OutputStream, imagePath: String) {
    try {
        val outputData = ByteArrayOutputStream()

        bmp.compress(CompressFormat.JPEG, 100, outputData)
        outputData.writeTo(outputStream)

        outputStream.flush()
        outputStream.close()

    } catch (e: IOException) {
        showBitmapWriteErrorMessage()
    }
}

I save the path while storing the image in internalStorgae the path looks like

/storage/emulated/0/Pictures/TGP AR/20211011142001.jpg and i pass this path into below method

To delete the image :

private fun deleteImage(imagePath: String) {
    val file = File(imagePath)
    file.delete()
}

file.exists() is returning true. file.delete() is returning false.

I think, there might be two different ways to delete ( > & < Q ). Please help me


Solution

  • You can delete the image by modifying your method to the following:

    private fun deleteImage(imagePath: Uri) { 
        getContentResolver().delete(imagePath, null, null)
    }
    

    Then pass the Uri created in generateUri() to delete the file.