【问题标题】:How to avoid "outofmemory" when download image in gridview?在gridview中下载图像时如何避免“内存不足”?
【发布时间】:2013-06-22 07:55:44
【问题描述】:

我试图在gridview中显示很多图像。有两个活动。它们都有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;
    }

}

【问题讨论】:

标签: android out-of-memory android-gridview


【解决方案1】:

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

你可以在 developer.android.com 上download 处理所有位图内存的一个非常好的示例。

【讨论】:

    【解决方案2】:

    使用延迟加载:这是一个很好的例子:https://github.com/nostra13/Android-Universal-Image-Loader

    【讨论】:

      【解决方案3】:

      我建议您遵循本教程。它一步一步 Tutorial

      【讨论】:

        猜你喜欢
        • 2014-02-19
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-12
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        相关资源
        最近更新 更多