coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.77k stars 659 forks source link

Support custom disk cache file naming strategies #1920

Open xVemu opened 11 months ago

xVemu commented 11 months ago

Is your feature request related to a problem? Please describe. Thumbnail in share intent must have an image extension, otherwise it's not shown.

Code sample ```kotlin @OptIn(ExperimentalCoilApi::class) private suspend fun share(context: Context) = withContext(Dispatchers.IO) { val intent = Intent().apply { action = Intent.ACTION_SEND putExtra(Intent.EXTRA_TEXT, "someLink") putExtra(Intent.EXTRA_TITLE, "someTitle") type = "text/plain" flags = Intent.FLAG_GRANT_READ_URI_PERMISSION } val request = ImageRequest.Builder(context) .data("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png") .build() context.imageLoader.execute(request) val uri = context.imageLoader.diskCache?.openSnapshot("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")?.use { snapshot -> FileProvider.getUriForFile( context, "pl.vemu.zsme.fileprovider", snapshot.data.toFile() ) } intent.clipData = ClipData(null, arrayOf("image/png"), ClipData.Item(uri)) val shareIntent = Intent.createChooser(intent, "Share post") withContext(Dispatchers.Main) { context.startActivity(shareIntent) } } ```

When I change snapshot.data.toFile() to a file, which is in the same directory, but has .png extension, everything works fine.

Describe the solution you'd like Add option in ImageLoader.Builder to preserve image extension in DiskCache.

colinrtwhite commented 11 months ago

I think this is best handled by adding a filenameFactory (or similar) param to DiskCache.Builder so it's possible to set a custom DiskCache.Key -> String function for creating the file name. That said, if you're sharing an image from Coil's file cache I'd recommend copying it locally as the file can disappear at any time.

xVemu commented 11 months ago

I don't see anything like that, DiskCache.Builder only allows changing directory or fileSystem.

colinrtwhite commented 11 months ago

Sorry, I was proposing a potential API as this doesn't exist yet. After thinking about this more we'd have to be careful adding this API as changing the filenameFactory's logic after files have already been added to the cache would corrupt the disk cache. I think we'd need to have migration/versioning support as well to make this safe.

xVemu commented 8 months ago

Workaround:

val uri = context.imageLoader.diskCache?.openSnapshot("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")?.use { snapshot ->
    val newFile = File.createTempFile("shareImage", ".png", File(context.cacheDir, "/image_cache"))

    snapshot.data.toFile().copyTo(newFile, true)
    FileProvider.getUriForFile(context, "pl.vemu.zsme.fileprovider", newFile)
}
titanseason commented 2 months ago

I think this is best handled by adding a filenameFactory (or similar) param to DiskCache.Builder so it's possible to set a custom DiskCache.Key -> String function for creating the file name. That said, if you're sharing an image from Coil's file cache I'd recommend copying it locally as the file can disappear at any time.

Any progress in adding filenameFactory?