compress Bitmap From Resource - Android Graphics

Android examples for Graphics:Bitmap Resource

Description

compress Bitmap From Resource

Demo Code


//package com.java2s;

import android.content.Context;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {

    public static Bitmap compressBitmapFromResourse(Context context,
            int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory/*from   w w  w.j a  v  a 2s  .  c o m*/
                .decodeResource(context.getResources(), resId, options);
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width
                    / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio
                    : widthRatio;
        }
        options.inSampleSize = inSampleSize;

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId,
                options);
    }
}

Related Tutorials