Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom disk cache file naming strategies #1920

Open
xVemu opened this issue Nov 13, 2023 · 4 comments
Open

Support custom disk cache file naming strategies #1920

xVemu opened this issue Nov 13, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@xVemu
Copy link

xVemu commented Nov 13, 2023

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
@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.

@xVemu xVemu added the enhancement New feature or request label Nov 13, 2023
@colinrtwhite
Copy link
Member

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
Copy link
Author

xVemu commented Nov 14, 2023

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

@colinrtwhite
Copy link
Member

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.

@colinrtwhite colinrtwhite changed the title Preserve image extension in DiskCache. Support custom disk cache file naming strategies Nov 16, 2023
@xVemu
Copy link
Author

xVemu commented Feb 19, 2024

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants