Bitmap没有明显的原因被回收

时间:2014-04-24 17:21:26

标签: android garbage-collection android-bitmap

我遇到了一个问题,如果我没有调用recycle()方法,或者内存使用方面存在任何问题(至少据我所知,我确实检查过设备监视器等),就会回收位图。

以下是相关代码:

首先在onCreate()我设置位图:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);

        ...

        mCurrentBitmap = MediaStorage.sCurrentBitmap;
        ...

然后我调用crop方法,并将mUndoBitmap设置为从mCurrentBitmap创建的新位图,这是我有回收问题的那个。

public void crop() {
        mUndoBitmap = Bitmap.createBitmap(mCurrentBitmap);
        Log.d("bitmap", Boolean.toString(mUndoBitmap.isRecycled()));
        ...
        call crop activity here
        ...
}

日志返回不回收位图。

然后我得到了活动结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == CROP_IMAGE_REQUEST_CODE) {
            if(resultCode == RESULT_OK) {
                Log.d("bitmap", Boolean.toString(mUndoBitmap.isRecycled()));
                mCurrentBitmap = MediaStorage.sCurrentBitmap;
                mTouchImageView.setImageBitmap(mCurrentBitmap);
                ...

仍然记录日志返回位图未被回收。

现在,有一个撤消按钮可以执行以下操作:

public void undo() {
        Log.d("bitmap", Boolean.toString(mUndoBitmap.isRecycled()));
        mRedoBitmap = mCurrentBitmap;
        mCurrentBitmap = mUndoBitmap;
        mTouchImageView.setImageBitmap(mCurrentBitmap);
        ...

日志返回该位图被回收,我得到“无法绘制回收位图”错误。

请注意,这三个方法是连续调用的,我的代码中没有任何地方可以在任何位图上调用recycle。所以,我的问题是: 有谁知道为什么我的位图被回收,我该如何防止它发生?

0 个答案:

没有答案