Skip to content

TiffConverter.convertJpgTiffFd fails to convert image with a CantOpenFileException from cache. #58

@pasllani

Description

@pasllani

I have a jetpack compose app with the following logic that triggers to allow user to select an image from the gallery and then copies it to cache:

val context = LocalContext.current
var progressString by remember { mutableStateOf<String?>(null) }
val launcher = rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) {
    if (it != null) {
        context.contentResolver.openInputStream(it)?.use { input ->
            val file = File(context.cacheDir, "copiedimage.jpg")
            FileOutputStream(file).use { output ->
                val buffer = ByteArray(4 * 1024)
                var read: Int
                while (input.read(buffer).also { read = it } != -1) {
                    output.write(buffer, 0, read)
                }
                output.flush()
            }
            val fd = context.contentResolver.openFileDescriptor(file.toUri(), "r")
            if (fd != null) {
                TiffConverterObject.convertToTiffAndSaveInCache(fd, context.cacheDir) { progress ->
                    progressString = "${progress * 100}%"
                }
            }
        }
    }
}

Button(
    modifier = Modifier
        .width(250.dp)
        .padding(bottom = 8.dp),
    onClick = {
        launcher.launch("image/*")
    }
) {
    Text(text = "Choose image from gallery")
}

I see the copiedimage.jpeg in the cache directory and I've saved it to my desktop and opened it so I know that's being saved correctly:
cache

This is my TiffConverterObject class which uses this library to convert the image to tiff but it keeps crashing returning a CantOpenFileException:

object TiffConverterObject {
    fun convertToTiffAndSaveInCache(inputFD: ParcelFileDescriptor, outputFD: File, progress: (Double) -> Unit) {
        val options = ConverterOptions()
        options.throwExceptions = true
        options.availableMemory = (128 * 1024 * 1024).toLong()
        options.compressionScheme = CompressionScheme.LZW
        options.appendTiff = false

        val file = File.createTempFile("output-temp", ".tiff", outputFD)
        val inputFd = inputFD.fd
        inputFD.close()

        val fd = App.instance.contentResolver.openFileDescriptor(file.toUri(), "rwt")
        if (fd != null) {
            val outputFd = fd.fd
            fd.close()

            val saved = TiffConverter.convertJpgTiffFd(inputFd, outputFd, options) { processedPixels, totalPixels ->
                progress(processedPixels.toDouble() / totalPixels)
            }
            
            println("\n\t-----> Saved: $saved")
        }
    }
}

I've tried all combinations of r,rw,rwt modes on both files as well as .jpg, .jpeg, .tif, and .tiff extensions with no luck. Any help would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions