0

I download photos from the Internet. have a site on the same page which has 18 photos (80x60 px ~ 10kb).

so I made ​​a list which loads the new picture (sleduyuschuyuyu page) the problem is when I load three or more pages, a memory error occurs the question is how to get rid of?

Now I build an array of bitmaps

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

and in adapter

@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;
    }

I was offered to save images in the cache and load them from there but do not know how to do it.

please help

3 Answers 3

1

Use disk cache... This article also has plenty of other useful tips for loading bitmaps http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#disk-cache

Sign up to request clarification or add additional context in comments.

Comments

0

The best way to load iomages is the picasso library at http://square.github.io/picasso/

Comments

0

You will read the images by using decoding the images to your desired size....

Convert the image into like this method.... It's used for drawable resources....

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;
    }

Which the below method for decode by the use of streams....

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

Suppose you will get fail in the all method You will try this logic you just need to add this line into your mainfest file...

You just need to add this line to your manifest file. It will allocate the large memory for your application.

    android:largeHeap="true"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.