compress Image to JPEG - Android Graphics

Android examples for Graphics:JPEG Image

Description

compress Image to JPEG

Demo Code


//package com.book2s;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class Main {

    public static Bitmap compressImage(Bitmap image) {
        int size = 100;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100;
        while (baos.toByteArray().length / 1024 > size) { 
            baos.reset();//  w w  w  . j  av  a 2  s. c o  m
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
            options -= 10;
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(
                baos.toByteArray());
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
        return bitmap;
    }
}

Related Tutorials