Imageview进行了recycle()之后再次打开加载同一图片遇到闪退

如题,由于优化内存,进行了bitmap.recycle,但是再次打开加载同一个activity的时候闪退。activity在finish的时候已经recycle,而且没有其他地方引用,居然会闪退。希望有更好的方法。求助,谢谢。代码如下:(setName之类的已经在merge中建立和声明)

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

public class DetailYS extends merge {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.merge);
    setName("  ys", Color.rgb(255,255,255));
    setBirthday("");
    setPhone("");
    setEmail("");
    setUni("");
    setPhoto(R.drawable.yangshun);
    findViewById(R.id.floatingActionButtonCall).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DetailYS.this.onDestroy();
            Intent intent = new Intent(DetailYS.this,MainActivity.class);
            startActivity(intent);
        }
    });
}

@Override
protected void onDestroy(){
    super.onDestroy();
    ImageView imageView = findViewById(R.id.app_bar_image);
    if(imageView!=null){
        Drawable drawable = imageView.getDrawable();
        if(drawable!=null){
            if (drawable instanceof BitmapDrawable){
                BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
                Bitmap bitmap = bitmapDrawable.getBitmap();
                if(bitmap!=null && !bitmap.isRecycled()){
                    bitmap.recycle();
                }
            }
        }
    }
    DetailYS.this.finish();

}
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode== KeyEvent.KEYCODE_BACK){
        DetailYS.this.finish();
        Intent intent = new Intent(DetailYS.this,MainActivity.class);
        startActivity(intent);
    }
    return true;
}

}


查看全部
zengtianyu12
zengtianyu12
2017/07/24 09:48
  • 图片
  • imageview
  • 优化
  • android
  • recycle
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

4个回复