我正在使用Palette从位图中获取vibrantSwatch颜色。
在编写此代码之前,我要从uri获取位图(在API 29中getBitmap已过时):
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),
Uri.fromFile(ImageModelArrayList.get(position).getImageUri()));
由于被弃用了,现在我编写了这段代码来获取位图:
ImageDecoder.Source source = ImageDecoder.createSource(context.getContentResolver(),
Uri.fromFile(ImageModelArrayList.get(position).getImageUri()));
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
现在在这个调色板代码中,如果我使用getBitmap没有问题,但是如果我使用ImageDecoder则会崩溃:
Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = p.getDominantSwatch();
Log.d(TAG, "onBindViewHolder: vibrantSwatch " + vibrantSwatch);
if (vibrantSwatch != null) {
holder.constraintLayout.setBackgroundColor(vibrantSwatch.getRgb());
}
错误:
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)