Skip to content

Commit

Permalink
Support loading ByteArrays. (#1202)
Browse files Browse the repository at this point in the history
* Support loading ByteArrays.

* Docs.
  • Loading branch information
colinrtwhite committed May 11, 2022
1 parent dccb0a8 commit d6ec2fe
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 0 deletions.
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

0 comments on commit d6ec2fe

Please sign in to comment.