Android ListView: out of memory exception

This question already has an answer here:

  • Strange out of memory issue while loading an image to a Bitmap object 40 answers

  • Use this function to decode image from sdcard path

    public Bitmap decodeFile(String path) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(path, o);
                // The new size we want to scale to
                final int REQUIRED_SIZE = 70;
    
                // Find the correct scale value. It should be the power of 2.
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;
    
                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeFile(path, o2);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return null;
    
        }
    

    How to use?

    holder.picView.setImageBitmap(decodeFile(act.getimage()));
    

    Note:

    you can change REQUIRED_SIZE value as per your need.


    That's a pretty common error. You are using a big image for each list element instead of use a small thumb...

    I use a lib called picasso to deal with images, check it out.

    Check this example.

    Picasso.with(YOUR_CONTEXT).load(new File(YOUR_PATH)).fit().centerCrop().into(YOUR_VIEW);
    

    use android-query to display images in listview.

    you can find tutorial of android-query here

    i hope it helps.

    链接地址: http://www.djcxy.com/p/93096.html

    上一篇: OutOfMemoryError使用位图setImageUri

    下一篇: Android ListView:内存不足异常