gpt4 book ai didi

android - 在gridview中下载图片时如何避免 "outofmemory"?

转载 作者:行者123 更新时间:2023-11-29 01:52:11 26 4
gpt4 key购买 nike

我试图在 gridview 中显示很多图像。有两个 Activity 。它们都有 gridviews,其中有图像。当我只启动其中一个时。没有问题,但是当我启动另一个时,存在“outifmemory”问题。PS:我第一次用stackoverflow,英语不好,见谅~.~

public class HeroGridAdapter extends BaseAdapter {

private Context mContext;
private int[] mImages;
private String[] mNames;
private String[] mShorts;
private LayoutInflater mInflater;
private LinearLayout.LayoutParams params;

private static Map<Integer,SoftReference<Bitmap>> imageCache = new HashMap<Integer,SoftReference<Bitmap>>();

public HeroGridAdapter(Context c, int[] images, String[] names,String[] shorts) {
this.mContext = c;
this.mImages = images;
this.mNames = names;
this.mShorts = shorts;
this.mInflater = LayoutInflater.from(c);
this.params = new LinearLayout.LayoutParams(45,45);
this.params.gravity = Gravity.CENTER;
}

@Override
public int getCount() {
return mImages.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewTag viewTag;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.hero_portrait_grid, null);
viewTag = new ItemViewTag(
(ImageView) convertView.findViewById(R.id.ivHeroPor),
(TextView) convertView.findViewById(R.id.tvHeroPor),
(TextView) convertView.findViewById(R.id.tvHeroShort));
convertView.setTag(viewTag);
} else {
viewTag = (ItemViewTag)convertView.getTag();
}
addBitmapToCache(mImages[position]);
viewTag.name.setText(mNames[position]);
viewTag.shortName.setText(mShorts[position]);
viewTag.icon.setImageBitmap(getBitmap(mImages[position]));
return convertView;
}

class ItemViewTag {
private ImageView icon;
private TextView name;
private TextView shortName;

public ItemViewTag(ImageView icon, TextView name,TextView shortName) {
this.icon = icon;
this.name = name;
this.shortName = shortName;
}
}

public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

public void addBitmapToCache(int resId){
Bitmap bitmap = decodeSampledBitmapFromResource(mContext.getResources(),
resId, 45, 45);
SoftReference<Bitmap> softBitmap = new SoftReference<Bitmap>(bitmap);
imageCache.put(resId, softBitmap);
}

public static Bitmap getBitmap(int imageKey){
SoftReference<Bitmap> softBitmap = imageCache.get(imageKey);
if(softBitmap == null){
return null;
}
Bitmap bitmap = softBitmap.get();
return bitmap;
}

}

最佳答案

我认为这可能对您有所帮助 displaying-bitmaps/manage-memory

并且有一个很好的样本你可以download在处理所有位图内存的 developer.android.com。

关于android - 在gridview中下载图片时如何避免 "outofmemory"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248585/

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