How to load an encrypted image from a URL? #2575
-
The image on the server is encrypted. I need to decrypt the binary data before decoding the image. Now that I have the function One way is to make a request in Composable function to get the ByteArray, and then use the decrypted ByteArray as the parameter of Platform is Compose Multiplatform (KTOR 3) on JVM |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Your best bet would probably be to extend the image pipeline with a custom |
Beta Was this translation helpful? Give feedback.
-
The issue has been resolved! Here is the code I used, hope it can help more people! private class CustomDecoder(
private val result: SourceFetchResult,
private val options: Options,
private val imageLoader: ImageLoader
) : Decoder {
override suspend fun decode(): DecodeResult? {
val defaultImageLoader = ImageLoader.Builder(options.context).build()
return defaultImageLoader.components.newDecoder(result, options, imageLoader)?.first?.decode()
}
}
class CustomDecoderFactory : Decoder.Factory {
override fun create(
result: SourceFetchResult,
options: Options,
imageLoader: ImageLoader
): Decoder {
val source = result.source
val buffer = Buffer().apply {
write(source.source().readByteArray().decryptImage()) // Decrypting ByteArray
}
val newSource = ImageSource(buffer, source.fileSystem, source.metadata)
val newResult = SourceFetchResult(newSource, result.mimeType, result.dataSource)
return CustomDecoder(newResult, options, imageLoader)
}
} @Composable
fun TestWidget() {
val context = LocalPlatformContext.current
val imageLoader = remember {
ImageLoader.Builder(context)
.components {
add(NetworkFetcher.Factory(
networkClient = { ImageEndpointClient.asNetworkClient() }
))
add(CustomDecoderFactory())
}
.build()
}
AsyncImage(
model = "https://example.com/xxx.jpg",
contentDescription = null,
imageLoader = imageLoader
)
} |
Beta Was this translation helpful? Give feedback.
Your best bet would probably be to extend the image pipeline with a custom
Decoder
that decrypts theSourceFetchResult
, creates a newSourceFetchResult
with the decrypted data, then delegates toimageLoader.components.newDecoder(...).first.decode()
.