【发布时间】:2016-02-12 12:03:22
【问题描述】:
我正在开发一个需要图片库的应用。我正在将所有图像显示在网格视图中。当我触摸一个图像时,我希望它用触摸的图像全屏打开一个活动,这也在工作,但是当我回到网格并打开另一个图像时,内存消耗不断增长。我尝试将特征图像设置为null、Bitmap.recycle() 并在活动上调用finish(),当我打开不同的图像时,这些似乎都无法阻止内存消耗的增加。
打开详细活动
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,并以这种模式循环。
欢迎任何提示/建议。
【问题讨论】:
-
看起来很正常。正在根据需要对对象进行垃圾收集。我会质疑在您的 OnPause 中调用 Finish,尽管旋转设备或接听电话等简单操作将完成活动。
-
谢谢@Kuffs。所以你觉得没有问题?我也会看看
onPause()问题。我没有考虑过,但是我的应用程序仅由于其他功能而具有便携性,并且我更喜欢在按下设备上的主页按钮后返回主屏幕而不是单独的图像。
标签: android performance memory-management bitmap android-memory