我认为你的异常从这里开始:
protected void throwIfCannotDraw(Bitmap bitmap) {
if (bitmap.isRecycled()) {
throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap);
}
if (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 &&
bitmap.hasAlpha()) {
throw new RuntimeException("Canvas: trying to use a non-premultiplied bitmap "
+ bitmap);
}
throwIfHwBitmapInSwMode(bitmap);
}
因此,或者您正在尝试使用回收的位图,或者您的 bmp 未预乘,或者您的硬件不支持位图
private void throwIfHwBitmapInSwMode(Bitmap bitmap) {
if (!mAllowHwBitmapsInSwMode && !isHardwareAccelerated()
&& bitmap.getConfig() == Bitmap.Config.HARDWARE) {
throw new IllegalStateException("Software rendering doesn't support hardware bitmaps");
}
}
但我不认为!
所以现在尝试按如下方式加载图像以避免错误并让我知道:
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
For 参考 https://developer.android.com/topic/performance/graphics/load-bitmap#read-bitmap.