Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: unable to getPixels(), pixel access is not supported on Config#HARDWARE bitmaps

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)
like image 238
Dnyaneshwar Avatar asked Feb 03 '23 15:02

Dnyaneshwar


2 Answers

You can copy the bitmap to a mutable one, not ideal but it works:

ImageDecoder.decodeBitmap(source).copy(Bitmap.Config.RGBA_F16, true)
like image 65
Beuz Avatar answered Feb 06 '23 14:02

Beuz


In Compose & Coil:

import coil.compose.rememberImagePainter

val imagePainter = rememberImagePainter(
                    data = product.imageUrl,
                    builder = {
                        crossfade(true)
                        placeholder(R.drawable.ic_placeholder)
                        allowHardware(false) //IMPORTANT!
                    }
                )
like image 24
Dr.jacky Avatar answered Feb 06 '23 14:02

Dr.jacky