Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void compressWithQuality(Bitmap image, String outPath, int maxSize) throws IOException {

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        image.compress(Bitmap.CompressFormat.JPEG, 20, os);

        while (os.toByteArray().length / 1024 > 1024) {
            os.reset();
            image.compress(Bitmap.CompressFormat.JPEG, 20, os);
        }

        ByteArrayInputStream bi = new ByteArrayInputStream(os.toByteArray());
        BitmapFactory.decodeStream(bi);
        FileOutputStream fi = new FileOutputStream(outPath);
        fi.write(os.toByteArray());
        fi.flush();
        fi.close();

    }
}