BitmapDrawable 千万不要使用bitmap.recycle

当在应用中使用BitmapDrawable时,直接调用Bitmap的recycle方法可能会导致运行时错误。文章详细讨论了这一问题及其原因,提醒开发者注意避免这种错误做法,以免影响应用的正常运行。
mImageVew = (ImageView) findViewById(R.id.imageView);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic);

//Bitmap.createScaledBitmap 以后原有的bitmap 可以recycle 了,不然会引起outofmemoryerror
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);

//BitmapDrawable 创建以后如果要是用的话,构造方法里面的bitmap 千万不要recycle 如果recycle 了就会报错
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), scaledBitmap);

bitmap.recycle();
//scaledBitmap.recycle();

mImageVew.setImageBitmap(bitmapDrawable.getBitmap());
//scaledBitmap.recycle();

 

如果recycle了回报如下的错误:

2019-10-10 07:59:48.640 17460-17460/bjpkten.handler E/AndroidRuntime: FATAL EXCEPTION: main
    Process: bjpkten.handler, PID: 17460
    java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@70c1569
        at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:62)
        at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:226)
        at android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:98)
        at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)
        at android.widget.ImageView.onDraw(ImageView.java:1342)
        at android.view.View.draw(View.java:20207)
        at android.view.View.updateDisplayListIfDirty(View.java:19082)
        at android.view.View.draw(View.java:19935)

 

public Drawable scaleWallpaper(Context context, Bitmap originalBitmap) { if (originalBitmap == null) { Log.d(TAG, "Invalid input: originalBitmap is null"); return null; } Bitmap scaledBitmap = null; Bitmap croppedBitmap = null; try { WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics(); if (windowManager != null) { windowManager.getDefaultDisplay().getRealMetrics(displayMetrics); } final int screenWidth = displayMetrics.widthPixels; final int screenHeight = displayMetrics.heightPixels; final int originalWidth = originalBitmap.getWidth(); final int originalHeight = originalBitmap.getHeight(); float scale = Math.max( (float) screenWidth / originalWidth, (float) screenHeight / originalHeight ); int scaledWidth = Math.round(originalWidth * scale); int scaledHeight = Math.round(originalHeight * scale); scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, scaledWidth, scaledHeight, true); int cropX = Math.max((scaledWidth - screenWidth) / 2, 0); int cropY = 0; int safeWidth = Math.min(screenWidth, scaledWidth); int safeHeight = Math.min(screenHeight, scaledHeight); croppedBitmap = Bitmap.createBitmap(scaledBitmap, cropX, cropY, safeWidth, safeHeight) .copy(Bitmap.Config.RGB_565, true); final Drawable resultDrawable = new BitmapDrawable(context.getResources(), croppedBitmap); if (originalBitmap != null && originalBitmap != croppedBitmap && !originalBitmap.isRecycled()) { originalBitmap.recycle(); } if (scaledBitmap != null && scaledBitmap != croppedBitmap && !scaledBitmap.isRecycled()) { scaledBitmap.recycle(); } return resultDrawable; } catch (Exception e) { Log.d(TAG, "Bitmap scale processing failed: " + e.getMessage()); return null; } }解释该段代码,当前获取壁纸有条纹怎么优化
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值