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 loading ByteArrays. #1202

Merged
merged 3 commits into from May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -220,6 +220,13 @@ class RealImageLoaderAndroidTest {
testExecute(data)
}

@Test
fun byteArray() = runTest {
val data = context.resources.openRawResource(R.drawable.normal).readBytes()
testEnqueue(data)
testExecute(data)
}

// endregion

@Test
Expand Down
2 changes: 2 additions & 0 deletions coil-base/src/main/java/coil/RealImageLoader.kt
Expand Up @@ -17,6 +17,7 @@ import coil.intercept.EngineInterceptor
import coil.intercept.RealInterceptorChain
import coil.key.FileKeyer
import coil.key.UriKeyer
import coil.map.ByteArrayMapper
import coil.map.FileUriMapper
import coil.map.HttpUrlMapper
import coil.map.ResourceIntMapper
Expand Down Expand Up @@ -86,6 +87,7 @@ internal class RealImageLoader(
.add(FileUriMapper())
.add(ResourceUriMapper())
.add(ResourceIntMapper())
.add(ByteArrayMapper())
// Keyers
.add(UriKeyer())
.add(FileKeyer(options.addLastModifiedToFileCacheKey))
Expand Down
9 changes: 9 additions & 0 deletions coil-base/src/main/java/coil/map/ByteArrayMapper.kt
@@ -0,0 +1,9 @@
package coil.map

import coil.request.Options
import java.nio.ByteBuffer

internal class ByteArrayMapper : Mapper<ByteArray, ByteBuffer> {

override fun map(data: ByteArray, options: Options): ByteBuffer = ByteBuffer.wrap(data)
}
1 change: 1 addition & 0 deletions coil-base/src/main/java/coil/request/ImageRequest.kt
Expand Up @@ -451,6 +451,7 @@ class ImageRequest private constructor(
* - [DrawableRes]
* - [Drawable]
* - [Bitmap]
* - [ByteArray]
* - [ByteBuffer]
*/
fun data(data: Any?) = apply {
Expand Down
1 change: 1 addition & 0 deletions coil-singleton/src/main/java/coil/Extensions.kt
Expand Up @@ -43,6 +43,7 @@ inline val Context.imageLoader: ImageLoader
* - [DrawableRes] [Int]
* - [Drawable]
* - [Bitmap]
* - [ByteArray]
* - [ByteBuffer]
*
* @param data The data to load.
Expand Down
1 change: 1 addition & 0 deletions docs/getting_started.md
Expand Up @@ -106,6 +106,7 @@ The base data types that are supported by all `ImageLoader` instances are:
* @DrawableRes Int
* Drawable
* Bitmap
* ByteArray
* ByteBuffer

## Supported Image Formats
Expand Down