get Compress Bitmap - Android android.graphics

Android examples for android.graphics:Bitmap Load Save

Description

get Compress Bitmap

Demo Code

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {

  public static Bitmap getCompressBitmap(String picPath, int toWidth, int toHeight) {

    BitmapFactory.Options op = new BitmapFactory.Options();
    op.inJustDecodeBounds = true;//from ww w .jav  a2  s  .co  m
    Bitmap comBitmap = BitmapFactory.decodeFile(picPath, op);
    int srcWidth = op.outWidth;
    int srcHeight = op.outHeight;
    int wRadio = 1, hRadio = 1;

    if (srcWidth < srcHeight) {
      if (srcWidth > toWidth || srcHeight > toHeight) {
        wRadio = (int) Math.ceil(op.outWidth / toWidth);
        hRadio = (int) Math.ceil(op.outHeight / toHeight);
      }

    } else {
      if (srcWidth > toWidth || srcHeight > toHeight) {
        wRadio = (int) Math.ceil(op.outWidth / toHeight);
        hRadio = (int) Math.ceil(op.outHeight / toWidth);
      }

    }

    if (wRadio >= 1 && hRadio >= 1) {
      if (wRadio > hRadio) {
        op.inSampleSize = wRadio;
      } else {
        op.inSampleSize = hRadio;
      }
    }
    op.inJustDecodeBounds = false;
    comBitmap = BitmapFactory.decodeFile(picPath, op);
    return comBitmap;
  }

}

Related Tutorials