Skip to content Skip to sidebar Skip to footer

Outofmemory Error In Bitmap Runtime Exception

i am displaying my images from assests/image folder , but this code is not working . this code display images from assets folder in gallery . i am using gallery prefine library or

Solution 1:

Hey please check my answer on the same issue: bitmap size exceeds Vm budget error android

And also always try to use maximum options while dealing with bitmaps like this:

 final Options options = new Options();
    options.outHeight = (int) scaleHeight; // new smaller height
    options.outWidth = (int) scaleWidth;   // new smaller width
    options.inScaled = true;
    options.inPurgeable = true;

    // to scale the image to 1/8
    options.inSampleSize = 8;
    bitmap = BitmapFactory.decodeFile(imagePath, options);

This might solve your problem.


Solution 2:

1) try to use bitmap.recycle(); to release memory before setting a new bitmap to your images

BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
if (bitmap != null)
{
    bitmap.recycle();
}

2) if your images are too large scale down them:

public static Bitmap decodeFile(File file, int requiredSize) {
        try {

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null, o);

            // The new size we want to scale to

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < requiredSize
                        || height_tmp / 2 < requiredSize)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;

            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file),
                    null, o2);

            return bmp;

        } catch (FileNotFoundException e) {
        } finally {
        }
        return null;
    }

Update

something like this:

for(int i=0; i<it.size();i++) { 
    ImageViewTouch imageView = new ImageViewTouch(GalleryTouchTestActivity.this); 
    imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    Options options = new Options(); 
    options.inSampleSize = 2;
    String photoURL = it.get(i);

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    if (bitmap != null)
    {
       bitmap.recycle();
    }

    bitmap = BitmapFactory.decodeFile(photoURL);

    imageView.setImageBitmap(bitmap); 
    arrayAdapter.add(imageView);
}

Post a Comment for "Outofmemory Error In Bitmap Runtime Exception"