我希望在Android上做一个全屏幕图片幻灯片,没有缩略图(如在画廊中),没有缩放,没有捏合。只有左/右滑动以从一张图片移动到另一张。
图片保存在数据文件夹中,共约2000张。
我使用全屏ViewPager来显示图片。当然,我无法一次加载所有2000张图片,所以只有在用户划到相应的视图时才加载所需的图片。
每次用户切换到新的ViewPager位置时,我调用这段代码:
public void onNewViewToDisplay(Integer newPosition, Integer previousPosition) {
File file = functionToGetTheCorrespondingFile(newPosition);
Bitmap bp;
bp = BitmapFactory.decodeFile(file.getPath());
((ImageView)imageAdapter.views.get(newPosition)).setImageBitmap(bp);
if (previousPosition != null && previousPosition != newPosition) {
BitmapDrawable bitmapDrawable =
((BitmapDrawable)((ImageView)imageAdapter.views.get(previousPosition)).getDrawable());
// Set the previous imageView to empty view
((ImageView) imageAdapter.views.get(previousPosition)).setImageResource(0);
// Then free previous bitmap memory
if (bitmapDrawable != null && bitmapDrawable.getBitmap() != null) {
bitmapDrawable.getBitmap().recycle();
}
}
}
因此,我希望我的代码始终释放先前显示的图像视图所使用的内存。但是当我查看我的内存占用情况时
导致了内存泄漏。我做错了什么?