compress Image using Bitmap compress method - Android Graphics

Android examples for Graphics:Bitmap Compress

Description

compress Image using Bitmap compress method

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap compressImage(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 80, baos);//100baos  
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) { //100kb,         
            baos.reset();//baosbaos  
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//options%baos  
            options -= 10;//10  
        }/*from w  ww .  jav  a2 s  .c  o m*/
        ByteArrayInputStream isBm = new ByteArrayInputStream(
                baos.toByteArray());//baosByteArrayInputStream  
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//ByteArrayInputStream  
        return bitmap;
    }
}

Related Tutorials