CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.
https://canhub.github.io/
Apache License 2.0
1.17k stars 240 forks source link

Managing cache file #608

Closed einhazard closed 5 months ago

einhazard commented 5 months ago

Sorry if I'm wrong, but is tmp_image_file created from your function too?

image

I don't want to save any cache file, I can delete cropped file after I use using code like this

private val cropImage = registerForActivityResult(CropImageContract()) { result ->
    if (result.isSuccessful) { 
        val uriContent = result.uriContent 
        val bitmap = MediaStore.Images.Media.getBitmap(myActivity.contentResolver, uriContent) 
        if (startImage == 1) {
            binding.imgData.setImageBitmap(image) 
        }

        val file = File(URI(uriContent.toString()))
        if (file.exists()) {
            val deleted = file.delete()
            if (deleted) {
                Log.e("HK", "file deleted")
            }  
        }  

    } 
}

but I can't find any of your function that make tmp_image_file. Where can I find it?

vanniktech commented 5 months ago

I think it is, but if you pass the URI yourself you can manage it yourself. This is also what I'm doing. The current tmp automatic creation is a relict which I haven't cleaned up yet.

LaurentLixfe commented 5 months ago

Hello, you should parse your cache directory too. Find any "tmp_image" or "cropped" file name and delete it at the end of your flows.

einhazard commented 5 months ago

Thanks, everyone, appreciate the suggestion

Based on @vanniktech suggestion to pass URI, I think I got what I wanted cleaner than deleting the file each time, but I don't know if this will be a problem in the future, I hope you guys can give me your comment

What I changed from your sample is instead of using createTempFile, I made the file using standard File

outputUri = null
private fun createImageFile(): File {
    val fileName = "${Koneksi.FP_FILE_NAME}${Koneksi.FP_FILE_FORMAT}"
    val storageDir: File? = myActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    val file = File(storageDir, fileName)

    val absolutePath = file.absolutePath
    Log.e("HK", "createImageFile path = $absolutePath")

    return file
}

And at CropImageContractOptions, I added outputUri on both uri and customOutputUri

cropImage.launch(
        CropImageContractOptions(
            uri = outputUri,
            cropImageOptions = CropImageOptions(
                imageSourceIncludeGallery = false,
                imageSourceIncludeCamera = true,
                guidelines = CropImageView.Guidelines.ON,
                outputCompressFormat = Bitmap.CompressFormat.JPEG,
                outputCompressQuality = 85,
                fixAspectRatio = true,
                aspectRatioX = 1,
                aspectRatioY = 1,
                customOutputUri = outputUri
                )
            ,
        ),
    )

everything else is still the same as the sample, and using the above code I made the app only make 1 file with the exact name that will be overwritten every time user does the cropping, so no additional code to delete the file needed

image

vanniktech commented 5 months ago

Yep that is also what I do