Library and just sample support for alternative BitmapLoaders
Open
@tonihei is already working on this.
Since Feb 2, 2024.
documentation candidate
enhancement
- Dominant language
- Java
- Stars
- 3k
- Forks
- 955
- Avg merge
- 12d 14h
- Merged PRs (30d)
- 2
Description
Use case description
Apps typically have some image loading library. These libraries may do a few things
- Cache images to avoid re-requesting
- Allow customising image loading, such as failure images, or transformations
- Use existing HTTP clients, including any interceptors, such as enforcing SSL.
Proposed solution
Show an example of using a typical image loader such as Coil.
/*
* Copyright 2022 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import androidx.media3.session.BitmapLoader
import coil.ImageLoader
import coil.request.ErrorResult
import coil.request.ImageRequest
import com.google.common.util.concurrent.ListenableFuture
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.guava.future
import java.io.IOException
class CoilBitmapLoader(
private val imageLoader: ImageLoader,
private val context: Context,
private val coroutineScope: CoroutineScope,
) : BitmapLoader {
override fun decodeBitmap(data: ByteArray): ListenableFuture<Bitmap> {
return coroutineScope.future {
val bitmap = BitmapFactory.decodeByteArray(data, /* offset= */0, data.size)
bitmap ?: throw IOException("Unable to decode bitmap")
}
}
override fun loadBitmap(uri: Uri): ListenableFuture<Bitmap> = coroutineScope.future {
val request = ImageRequest.Builder(context)
.data(uri)
.allowHardware(false)
.build()
val response = imageLoader.execute(request)
val bitmap = (response.drawable as? BitmapDrawable)?.bitmap
bitmap
?: throw IOException("Unable to load bitmap " + (response as? ErrorResult)?.throwable)
}
}
and in MediaSessionService
override fun onCreate() {
super.onCreate()
setMediaNotificationProvider(DefaultMediaNotificationProvider(application, CoilBitmapLoader(
ImageLoader(this), application, this.lifecycleScope)))
}
Alternatives considered
This is already possible, feel free to close, or advise if the code above is correct.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.