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:

finalOptionsoptions=newOptions();
    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

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

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

publicstatic Bitmap decodeFile(File file, int requiredSize) {
        try {

            // Decode image size
            BitmapFactory.Optionso=newBitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(newFileInputStream(file), null, o);

            // The new size we want to scale to// Find the correct scale value. It should be the power of 2.intwidth_tmp= o.outWidth, height_tmp = o.outHeight;
            intscale=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.Optionso2=newBitmapFactory.Options();
            o2.inSampleSize = scale;

            Bitmapbmp= BitmapFactory.decodeStream(newFileInputStream(file),
                    null, o2);

            return bmp;

        } catch (FileNotFoundException e) {
        } finally {
        }
        returnnull;
    }

Update

something like this:

for(int i=0; i<it.size();i++) { 
    ImageViewTouchimageView=newImageViewTouch(GalleryTouchTestActivity.this); 
    imageView.setLayoutParams(newGallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    Optionsoptions=newOptions(); 
    options.inSampleSize = 2;
    StringphotoURL= it.get(i);

    BitmapDrawabledrawable= (BitmapDrawable) imageView.getDrawable();
    Bitmapbitmap= 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"