-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Expose an API for converting DrawableResource -> ByteArray #4360
Comments
Thanks! We will discuss it. |
Hi @colinrtwhite ! I have an idea to add a function |
@terrakok Yep, I think that would work! Thinking out loud this is how Coil would use it:
|
Hi @colinrtwhite! I returned to the issue and think there is a problem: when we try to convert a DrawableResource to the Uri string, we do this depending on a current environment. It is a hidden logic which can leak to user's code. The uri string is static and points on concrete file instead of a dynamic accessor with the environment handling. fun DrawableResource.toUri(): String {
val resource = this
val resourceReader = LocalResourceReader.current
val environment = LocalComposeEnvironment.current.rememberEnvironment()
val resourceItem = remember(resource, environment) { resource.getResourceItemByEnvironment(environment) }
return resourceReader.getUri(resourceItem.path)
} So, I will add the uri getter only for files by path: |
Adds a public `Res.getUri(path: String): String` function. It lets external libraries a way to read resource files by a platform dependent Uri. E.g.: video players, image loaders or embedded web browsers. ```kotlin val uri = Res.getUri("files/my_video.mp4") ``` fixes #4360
@terrakok Thanks for the update. Unfortunately, I don't think this solves the use case as Also it sounds like it won't be possible to load the generated |
I don't say the coil calls AsyncImage(
model = Res.getUri("files/my_photo.jpg"),
contentDescription = null,
) Regarding type safe drawable references, could you explain why the following approach doesn't work for you? AsyncImage(
model = painterResource(Res.drawable.some_icon),
contentDescription = null,
) |
@terrakok Ah sorry - passing For |
…ources. (#4651) Implemented two new experimental functions: ```kotlin /** * Retrieves the byte array of the drawable resource. * * @param environment The resource environment, which can be obtained from [rememberResourceEnvironment] or [getSystemResourceEnvironment]. * @param resource The drawable resource. * @return The byte array representing the drawable resource. */ @ExperimentalResourceApi suspend fun getDrawableResourceBytes( environment: ResourceEnvironment, resource: DrawableResource ): ByteArray {...} /** * Retrieves the byte array of the font resource. * * @param environment The resource environment, which can be obtained from [rememberResourceEnvironment] or [getSystemResourceEnvironment]. * @param resource The font resource. * @return The byte array representing the font resource. */ @ExperimentalResourceApi suspend fun getFontResourceBytes( environment: ResourceEnvironment, resource: FontResource ): ByteArray {...} ``` fixes #4360
Hi I'm looking to add support for the new multiplatform resources API in Coil so it's possible to do:
Using
AsyncImage
would have a couple benefits vs. usingImage
andpainterResource
:Decoder
s to decode custom file types.AsyncImage
is a unified API that supports the file system and network URLs too.At the moment I don't think it's possible to add support as the APIs to fetch a resource (
readResourceBytes
,Resource.getPathByEnvironment
, andResourceEnvironment
) are internal.Res.readBytes
is public, but it's generated so it can't easily be used in libraries.Res.readBytes
also requires a path, but there's no public method to convert aDrawableResource
into a path.I think exposing
LocalComposeEnvironment.current.rememberEnvironment()
andresource.getPathByEnvironment(environment)
(basically whateverpainterResource(resource: DrawableResource)
uses) would be enough to support this, but let me know what you think!The text was updated successfully, but these errors were encountered: