0

I currently have a decode bitmap method to optimize the size of the image file, however the input file won’t be a bitmap and will decode to bitmap afterward. So my problem is, how can I resize it if my input is a bitmap already? Thanks.

private Bitmap compressFile(File f) {
    int REQUIRED_SIZE = 80;
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // 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 (REQUIRED_SIZE > 0) {
            if (width_tmp <= REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            // height_tmp /= 2;
            scale++;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

What I would like to change to is compressFile (Bitmap f)

1
  • are you loading image from internet or choosing from gallery? Commented Aug 14, 2013 at 11:58

1 Answer 1

2

You may use this too

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

or are you asking for:

bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);

check this link for more info

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

1 Comment

this will gives outOfMemory error for a high resolution images

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.