我正在開發需要圖庫的應用程序。我在所有圖像顯示在網格視圖中的點。當我觸摸圖像時,我希望它能夠以全屏方式打開一個觸摸圖像的活動,這也正在工作,但是當我回到網格並打開另一個圖像時,內存消耗量不斷增加。我曾嘗試將功能圖像設置爲null
,Bitmap.recycle()
,並調用finish()
這些活動,當我打開不同的圖像時,這些活動似乎都無法阻止內存消耗的增加。Android在離開活動時從內存中刪除位圖
打開詳細活動
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("image", adapter.file[position].getPath());
//Start details activity
startActivity(intent);
特徵的圖像的活動
public class DetailActivity extends AppCompatActivity {
private Intent intent = getIntent();
private Bitmap featureImage;
private ImageView featureView;
private final String TAG ="Image Detail --";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
final String imgPath = extras.getString("image");
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inDensity = 2;
bitmapOptions.inTargetDensity = 1;
featureImage = BitmapFactory.decodeFile(imgPath, bitmapOptions);
featureView = (ImageView) findViewById(R.id.featureImageView);
featureView.setImageBitmap(featureImage);
}
@Override
protected void onPause(){
super.onPause();
//featureImage = null;
//featureView.setImageBitmap(null);
//featureImage.recycle();
this.finish();
}
}
存儲器繼續熟料高達約40MB,然後下降到在這種模式32MB和循環throgh。
歡迎提出任何建議。
看起來很正常。對象正在根據需要進行垃圾回收。我會質疑在OnPause中調用完成,但作爲一個簡單的操作,例如旋轉設備或接聽電話將完成活動。 – Kuffs
謝謝@Kuffs。所以你認爲沒有問題?我也會看'onPause()'問題。我並沒有對此表示贊同,但是我的應用只是由於其他功能而被移植,我更喜歡在設備上的主頁按鈕被按下後返回到主屏幕而不是單個圖像。 – Lonergan6275