0

我正在编写一个将字符串存储在 SQLite 数据库中的应用程序,它表示 /sdcard/ 上图像的文件路径。我在我的一项活动中有此代码onCreate()

final Intent receivedIntent = getIntent();

String imageStr = receivedIntent.getExtras().getString("picture");
ImageView imageView = (ImageView) findViewById(R.id.pPicture);
File file = new File (imageStr);
if (file.exists()) {
   Bitmap bitmap = BitmapFactory.decodeFile(imageStr);
   imageView.setImageBitmap(bitmap);
}

该代码在我第一次加载活动时有效,但是当我在屏幕方向之间切换时,它会使我的应用程序崩溃。关于我能做些什么来解决这个问题的任何想法?我希望蜜蜂能够继续在方向之间切换,但我不需要每次都刷新。

另外,我有点新,所以如果可能的话,请尽量让你的答案不要太复杂。

请注意,该文件在所有情况下都存在。

4

1 回答 1

3

我认为您的应用程序崩溃是因为OutOfMemoryException. 尝试在以下位置回收位图onDestroy()

@Override
public void onDestroy() {
    super.onDestroy();

    ImageView imageView = (ImageView) findViewById(R.id.pPicture);
    Drawable drawable = imageView.getDrawable();
    imageView.setImageDrawable(null);

    if (drawable instanceof BitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        bitmap.recycle();
        bitmap = null;
    }
}
于 2011-08-30T16:47:03.703 回答