Android 内存不足+;位图+;ImageView+;寻呼机
我正在为我的应用开发一个简单的照片库。但是我有一些非常讨厌的内存泄漏,我不知道如何处理它们 你们能帮帮我吗?我搜索了另一个问题,但根据答案,我似乎在做正确的事情。当然,我不是 我的代码: 我用它来显示图像 此外,我还扩展了Android 内存不足+;位图+;ImageView+;寻呼机,android,bitmap,android-viewpager,out-of-memory,touchimageview,Android,Bitmap,Android Viewpager,Out Of Memory,Touchimageview,我正在为我的应用开发一个简单的照片库。但是我有一些非常讨厌的内存泄漏,我不知道如何处理它们 你们能帮帮我吗?我搜索了另一个问题,但根据答案,我似乎在做正确的事情。当然,我不是 我的代码: 我用它来显示图像 此外,我还扩展了ViewPager以支持缩放功能,如下所示: public class ExtendedViewPager extends ViewPager { public ExtendedViewPager(Context context) { super(co
ViewPager
以支持缩放功能,如下所示:
public class ExtendedViewPager extends ViewPager {
public ExtendedViewPager(Context context) {
super(context);
}
public ExtendedViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof TouchImageView) {
/*
canScrollHorizontally is not supported for Api < 14. To get around this issue,
ViewPager is extended and canScrollHorizontallyFroyo, a wrapper around
canScrollHorizontally supporting Api >= 8, is called.
*/
return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
}
并在我的PagerAdapter
实现中调用它,在该实现中,我还尝试recycle()
位图:
private class TouchImagePagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return galleryImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = PhotoGalleryActivity.this;
TouchImageView touchImageView = new TouchImageView(context);
touchImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
touchImageView.setImageBitmap(Helper.resample(galleryImages[position].getImgResource(), false, getApplicationContext()));
container.addView(touchImageView, 0);
return touchImageView;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object object) {
TouchImageView ti = (TouchImageView) object;
BitmapDrawable bmpDrawable = (BitmapDrawable) ti.getDrawable();
if (bmpDrawable != null) {
Bitmap bmp = bmpDrawable.getBitmap();
if(bmp != null && !bmp.isRecycled())
bmp.recycle();
}
collection.removeView(ti);
ti = null;
}
@Override
public void destroyItem(View collection, int position, Object o) {
View view = (View)o;
((ViewPager) collection).removeView(view);
view = null;
}
}
有什么线索吗?
提前感谢。如果原始bmp大小大于imageview,我建议更改inSampleSize 2或4。你说有时候图像大于2000x2000。这可能会导致内存问题。在标记中的项目清单文件中添加以下行 android:largeHeap=“true” 例如:
<application
android:name="com......"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/CustomActionBarTheme">
这有助于为应用程序提供更多的内存,但我希望使用全屏显示这些图像。使用这些值,图像将太小。希望这能解决此问题。请参考此以减少位图这不是我需要做的,但注释下方的选项
//此选项允许android在内存不足时声明位图内存
帮助。Outofmemory
错误出现的时间更长。但它没有修复它。这可能会有所帮助,但它无法修复内存泄漏。最后,会弹出Outofmemory
错误。
<application
android:name="com......"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/CustomActionBarTheme">