I'm getting the vibrantSwatch color from bitmap using Palette.
To get bitmap from uri before I wrote this code(In API 29 getBitmap has depricated) :
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),
Uri.fromFile(ImageModelArrayList.get(position).getImageUri()));
Because of deprication I wrote this code now to get bitmap:
ImageDecoder.Source source = ImageDecoder.createSource(context.getContentResolver(),
Uri.fromFile(ImageModelArrayList.get(position).getImageUri()));
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
Now here in this Palette code I'm getting crash(If I use getBitmap no issue. If I use ImageDecoder I'm getting crash):
Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = p.getDominantSwatch();
Log.d(TAG, "onBindViewHolder: vibrantSwatch " + vibrantSwatch);
if (vibrantSwatch != null) {
holder.constraintLayout.setBackgroundColor(vibrantSwatch.getRgb());
}
Error:
2020-02-29 12:32:56.722 9865-9865/com.msp.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.msp.project, PID: 9865
java.lang.IllegalStateException: unable to getPixels(), pixel access is not supported on Config#HARDWARE bitmaps
at android.graphics.Bitmap.checkHardware(Bitmap.java:401)
at android.graphics.Bitmap.getPixels(Bitmap.java:1760)
You can copy the bitmap to a mutable one, not ideal but it works:
ImageDecoder.decodeBitmap(source).copy(Bitmap.Config.RGBA_F16, true)
In Compose & Coil:
import coil.compose.rememberImagePainter
val imagePainter = rememberImagePainter(
data = product.imageUrl,
builder = {
crossfade(true)
placeholder(R.drawable.ic_placeholder)
allowHardware(false) //IMPORTANT!
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With