Android O:DragShadowBuilder引发IllegalStateException:软件渲染不支持硬件位图 [英] Android O: DragShadowBuilder throws IllegalStateException: Software rendering doesn't support hardware bitmaps

查看:179
本文介绍了Android O:DragShadowBuilder引发IllegalStateException:软件渲染不支持硬件位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用允许用户在活动中拖放图像.在Android O和硬件图像加速到来之前,它一直可以正常工作.该代码现在引发以下异常:

My Android app allows users to drag and drop images in an activity. It used to work fine until Android O and hardware image acceleration arrived. The code throws the following exception now:

IllegalStateException:软件渲染不支持硬件位图

IllegalStateException: Software rendering doesn't support hardware bitmaps

这是我的代码:

public class PhotoDragShadowBuilder extends DragShadowBuilder {
    @Override
    public void onDrawShadow(Canvas canvas) {
        Drawable photoDrawable = imageView.getDrawable();
        photoDrawable.draw(canvas);
    }
}

imageView.getDrawable()内的位图设置了标志 Bitmap.Config.HARDWARE .这意味着位图仅存储在硬件中.Android拒绝将位图绘制到存储在堆中的画布上.

The Bitmap inside of imageView.getDrawable() has the flag Bitmap.Config.HARDWARE set. It means that the bitmap is only stored in hardware. Android refuses to draw the Bitmap to the canvas that's stored in heap.

该异常的负责任的Android源代码在这里:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/BaseCanvas.java#532

The responsible Android source code for the exception is here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/BaseCanvas.java#532

很明显,我可以再次加载位图以进行拖放.但是,这对于设备来说是额外的工作,而且还需要将许多很好解耦的代码捆绑在一起.

Obviously, I could load the Bitmap a second time for dragging and dropping. However, that's extra work for the device plus requires tying together a lot of nicely decoupled code.

是否可以解决此问题?有没有办法将位图从硬件中移出并放入堆中?

Is there a way to fix this? Is there a way to get the Bitmap out of the hardware and into heap?

推荐答案

ANDROID O的解决方法导致错误"IllegalStateException:软件渲染不支持硬件位图".

Workaround for ANDROID O causes an error"IllegalStateException: Software rendering doesn't support hardware bitmaps".

重新编码公共类GlideConfiguration扩展了AppGlideModule,如下所示:

Recode your public class GlideConfiguration extends AppGlideModule as below:

    RequestOptions options = new RequestOptions();
    boolean isAndroidO = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O);
    DecodeFormat format = isAndroidO ? DecodeFormat.PREFER_ARGB_8888 : DecodeFormat.PREFER_RGB_565;
    options.format(format);
    if (isAndroidO) options.disallowHardwareConfig();

    builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));
    builder.setDefaultRequestOptions(options);

这篇关于Android O:DragShadowBuilder引发IllegalStateException:软件渲染不支持硬件位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆