我正在使用 Palette 从位图中获取鲜艳的Swatch 颜色。
在编写此代码之前从 uri 获取位图(在 API 29 中,getBitmap 已弃用):
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),
Uri.fromFile(ImageModelArrayList.get(position).getImageUri()));
Run Code Online (Sandbox Code Playgroud)
由于 deprication 我现在写了这段代码来获取位图:
ImageDecoder.Source source = ImageDecoder.createSource(context.getContentResolver(),
Uri.fromFile(ImageModelArrayList.get(position).getImageUri()));
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
Run Code Online (Sandbox Code Playgroud)
现在在这个 Palette 代码中,我崩溃了(如果我使用 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());
}
Run Code Online (Sandbox Code Playgroud)
错误:
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 …Run Code Online (Sandbox Code Playgroud)