Pages

Wednesday, August 8, 2012

How to Compress Image

How to Compress Image

When big size image is came to display in Imageview then java.lang.OutOfMemoryError: bitmap size exceeds VM budget error occur to handle this error we have to manage(compress) the each manage. 

For that two way is there to Compress the image

 

1) If the Height & Width is more then Imageview then we have to compress both.

2) If footprint is more then we have to compress it.

 

For that following code is possible:-

public Bitmap checkBitmap(int pos, int h, int w) {
        Bitmap bitmap = null;
        Options option = new Options();
        // this part is for Height and Width manage...
        option.inJustDecodeBounds = true;
        int sample = 16;

        BitmapFactory.decodeFile(MainActivity.strImagFileList.get(pos), option);

        if (option.outHeight > h || option.outWidth > w) {
            if (option.outHeight > option.outWidth) {
                sample = option.outHeight / h;
            } else {
                sample = option.outWidth / w;
            }
            System.out.println("Hello");
        }
        option.inSampleSize = sample;
        // this part is for compress footprint  manage...
        option.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(
                MainActivity.strImagFileList.get(pos), option);
        return bitmap;
    } 

No comments:

Post a Comment

Must Comment