gpt4 book ai didi

android - 如何避免从android资源加载大图片时出现OOM

转载 作者:行者123 更新时间:2023-12-01 22:28:20 30 4
gpt4 key购买 nike

我已经开发了 20 多个 Android 应用程序,应用程序有一个由大图像幻灯片(例如 4 张尺寸为 750 x 1334 的图像)组成的教程 Activity ,并将在首次启动时向用户显示。

由于图像质量的原因,我无法再减小尺寸。

我的代码 fragment 如下;

public class GalleryImageActivity extends BaseActivity implements OnGestureListener, OnTouchListener {
ViewPager imagePager;
GalleryImageAdapter galleryImageAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_image);

imagePager = (ViewPager) findViewById(R.id.image_pager);

galleryImageAdapter = new GalleryImageAdapter(this);
imagePager.setAdapter(galleryImageAdapter);
imagePager.setOnTouchListener(this);
}
}


public class GalleryImageAdapter extends PagerAdapter {
public static int SCREEN_CNT = 4;
Bitmap[] bmp = new Bitmap[SCREEN_CNT];

@Override
public int getCount() {
return SCREEN_CNT;
}

@Override
public Object instantiateItem(ViewGroup view, int position) {
View view = inflater.inflate(R.layout.gallery_image_item, null);
ImageView imv = (ImageView) view.findViewById(R.id.imgView);

bmp[position] = readBitMap(context, R.drawable.tutorial_img_0 + position, position);
if (bmp[position] != null)
imv.setImageBitmap(bmp[position]);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

if (bmp[position] != null && !bmp[position].isRecycled())
{
bmp[position].recycle();
System.gc();
bmp[position] = null;
}
}

public Bitmap readBitMap(Context context, int resId, int position) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;

InputStream is = context.getResources().openRawResource(resId);
BitmapFactory.decodeStream(is,null, opt);
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
opt.inJustDecodeBounds = false;

try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}

is = context.getResources().openRawResource(resId);
Bitmap bitmap = BitmapFactory.decodeStream(is,null, opt);

try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}

return bitmap;
}
}

有时效果很好,但是当重复5~6次时,BitmapFactory.decodeStream会抛出“内存不足”。我如何决定比例大小或解决此问题?

示例图像如下; enter image description here

最佳答案

您可以修改readBitMap函数如下;

public Bitmap readBitMap(Context context, int resId, int position){ 
if (bmp[position] == null || bmp[position].isRecycled())
{
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;

opt.inPurgeable = true;
opt.inInputShareable = true;

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
opt.inScaled = true;
opt.inDensity = DisplayMetrics.DENSITY_DEFAULT; // !important

InputStream is = context.getResources().openRawResource(resId);
try {
bmp[position] = BitmapFactory.decodeStream(is,null,opt);
} catch (Exception e){
bmp[position] = null;
}
}

return bmp[position];
}

某些类型的手机,尤其是三星手机,对于内存泄漏的设计并不完善。

opt.inDensity = DisplayMetrics.DENSITY_DEFAULT非常重要。

如果inDensity0,则BitmapFactory.decodeStream将填充与资源关联的密度。 more...

当您的 Activity(或页面)被销毁时,您应该将 imageview 的位图设置为 null

关于android - 如何避免从android资源加载大图片时出现OOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52195458/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com