Android 加载图像列表视图时的outOfMemory

Android 加载图像列表视图时的outOfMemory,android,out-of-memory,Android,Out Of Memory,我从网上下载照片。在同一页面上有一个站点,其中有18张照片(80x60像素~10kb) 所以我做了​​加载新图片的列表(SLEDUYUSCHUYOU页面) 问题是,当我加载三个或更多页面时,会发生内存错误 问题是如何摆脱 现在我构建一个位图数组 for (Element titles : title) { if (titles.children().hasClass("btl")){ m = new HashMap&

我从网上下载照片。在同一页面上有一个站点,其中有18张照片(80x60像素~10kb)

所以我做了​​加载新图片的列表(SLEDUYUSCHUYOU页面) 问题是,当我加载三个或更多页面时,会发生内存错误 问题是如何摆脱

现在我构建一个位图数组

for (Element titles : title) {
                    if (titles.children().hasClass("btl")){
                    m = new HashMap<String, Object>();
                    m.put(MyActivity.ATTRIBUTE_NAME_TEXT, titles.select("a[href]").attr("abs:href"));
                    Picasso p = Picasso.with(MyActivity.context);
                    m.put(MyActivity.ATTRIBUTE_NAME_PHOTO,Bitmap.createScaledBitmap(p.load(titles.select("img").attr("abs:src")).get(),80,60, true) );
                    data.add(m);
                    }
                }
for(元素标题:标题){
if(titles.children().hasClass(“btl”)){
m=新的HashMap();
m、 放置(MyActivity.ATTRIBUTE\u NAME\u TEXT,titles.select(“a[href]”).attr(“abs:href”);
毕加索p=Picasso.with(MyActivity.context);
m、 put(MyActivity.ATTRIBUTE\u NAME\u PHOTO,Bitmap.createScaledBitmap(p.load(titles.select(“img”).attr(“abs:src”)).get(),80,60,true));
数据。添加(m);
}
}
和在适配器中

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final Map<String, Object> itemData = datas.get(position*2);

        final Map<String, Object> itemData2 = datas.get(position*2+1);
        Bitmap bitmap2 = null;
        Bitmap bitmap = (Bitmap) itemData.get("img");
        if(itemData2!=null)
            bitmap2 = (Bitmap) itemData2.get("img");

        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.items, null, true);
            holder = new ViewHolder();
            holder.ivImage = (ImageView) rowView.findViewById(R.id.imageView);
            holder.ivImage2 = (ImageView) rowView.findViewById(R.id.imageView1);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }
        holder.ivImage.setImageBitmap(bitmap);
        holder.ivImage2.setImageBitmap(bitmap2);
        holder.ivImage.setTag(position*2);
        holder.ivImage2.setTag(position*2+1);
        holder.ivImage.setOnClickListener(this);
        holder.ivImage2.setOnClickListener(this);
        return rowView;
    }
@覆盖
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
最终地图itemData=datas.get(位置*2);
最终地图itemData2=datas.get(位置*2+1);
位图bitmap2=null;
位图位图=(位图)itemData.get(“img”);
if(itemData2!=null)
bitmap2=(位图)itemData2.get(“img”);
视图行视图=转换视图;
if(rowView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
rowView=充气机。充气(R.layout.items,null,true);
holder=新的ViewHolder();
holder.ivImage=(ImageView)rowView.findViewById(R.id.ImageView);
holder.ivImage2=(ImageView)rowView.findViewById(R.id.imageView1);
rowView.setTag(支架);
}否则{
holder=(ViewHolder)rowView.getTag();
}
holder.ivImage.setImageBitmap(位图);
holder.ivImage2.setImageBitmap(位图2);
支架ivImage setTag(位置*2);
支架.ivImage2.setTag(位置*2+1);
holder.ivImage.setOnClickListener(此);
holder.ivImage2.setOnClickListener(此);
返回行视图;
}
我被要求在缓存中保存图像并从那里加载它们,但我不知道怎么做


请帮助

加载iMoges的最佳方法是在

使用磁盘缓存。。。本文还提供了大量其他加载位图的有用提示

您将通过将图像解码到所需大小来读取图像

像这样将图像转换为。。。。 它用于可提取资源

Bitmap bmap2 = decodeSampledBitmapFromResource(getResources(),
     R.drawable.hydrangeas, width, height);  


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;
        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 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) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
下面使用流进行解码的方法

public static Bitmap decodeSampledBitmapFromResource(InputStream in,
        int reqWidth, int reqHeight) throws IOException {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    in.mark(in.available());
    BitmapFactory.decodeStream(in, null, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    in.reset();
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(in, null, options);
}
假设您将在all方法中失败,您将尝试此逻辑,您只需要将此行添加到mainfest文件中

您只需将此行添加到清单文件中即可。它将为您的应用程序分配大内存。

    android:largeHeap="true"