Compressor icon indicating copy to clipboard operation
Compressor copied to clipboard

FileNotFound Exception When i try to .compressToFile(imageUri)

Open HassanAhamd opened this issue 4 years ago • 2 comments

fun compressor(imageFilePath: File) {
        val compressor = Compressor(this@AddNewStudentActivity)
        Log.e("ImageFile Path","${imageFilePath}")
        var compressedImage = compressor.setDestinationDirectoryPath(path.toString())
            .setMaxWidth(120)
            .setMaxHeight(150)
            .setQuality(40)
            .setCompressFormat(Bitmap.CompressFormat.JPEG)
            .compressToFile(imageFilePath)
    var compresseduri =  Uri.fromFile(compressedImage)
    mBinding.selectStudentsImageImageView.setImageURI(compresseduri)

}

HassanAhamd avatar Aug 09 '21 10:08 HassanAhamd

fun compressor(imageFilePath: File) {
        val compressor = Compressor(this@AddNewStudentActivity)
        Log.e("ImageFile Path","${imageFilePath}")
        var compressedImage = compressor.setDestinationDirectoryPath(path.toString())
            .setMaxWidth(120)
            .setMaxHeight(150)
            .setQuality(40)
            .setCompressFormat(Bitmap.CompressFormat.JPEG)
            .compressToFile(imageFilePath)
    var compresseduri =  Uri.fromFile(compressedImage)
    mBinding.selectStudentsImageImageView.setImageURI(compresseduri)

}

Did you find any solution for this issue ? Is this device specific issue ? On which device did you try this ?

pradeep-orbi avatar Feb 06 '22 05:02 pradeep-orbi

I know this is a bit old, but just in case it helps anyone.

If you are trying to save to a file that does not have an extension, then it is affected by a bug in Util.kt:

val result = if (format == imageFile.compressFormat()) {
        imageFile
    } else {
        File("${imageFile.absolutePath.substringBeforeLast(".")}.${format.extension()}")
    }

This logic does not consider the potential of a file destination that has no extension, so instead of /data/my.app.id/cache/compressor - we get /data/my.imagename.extension - which doesn't work.

The destination must have a file extension. The extension does not need to be related to the image format at all, but there must be an extension.

mikedawson avatar Dec 28 '23 17:12 mikedawson