compress Image By Length - Android Media

Android examples for Media:Picture

Description

compress Image By Length

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;

public class Main {
    public static ByteArrayOutputStream compressImageByLength(Bitmap image,
            int maxLength) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
        int options = 100;
        while (baos.toByteArray().length / 1024 > maxLength) { 
            baos.reset();/*w w w .j ava 2  s  .  c om*/
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
            options -= 5;
        }

        return baos;
    }
}

Related Tutorials