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)

 

你可能感兴趣的:(Android,开发点滴)