Example usage for android.graphics Bitmap compress

List of usage examples for android.graphics Bitmap compress

Introduction

In this page you can find the example usage for android.graphics Bitmap compress.

Prototype

@WorkerThread
public boolean compress(CompressFormat format, int quality, OutputStream stream) 

Source Link

Document

Write a compressed version of the bitmap to the specified outputstream.

Usage

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, String filepath) {
    File f = new File(filepath);
    FileOutputStream out = null;/*from w  ww.  j ava 2s .c  o  m*/
    try {
        out = new FileOutputStream(f);

        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (FileNotFoundException e) {
        return false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }

    return true;
}

From source file:Main.java

public static Bitmap compressByLimit(Bitmap bitmap, int limitSize) {
    if (bitmap == null) {
        return null;
    }/*from   ww  w.  j  ava2s.  c  om*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    while (baos.size() > limitSize && options > 0) {
        options--;
        baos.reset();
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }
    byte[] compressedData = baos.toByteArray();
    return BitmapFactory.decodeByteArray(compressedData, 0, compressedData.length);
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap, Bitmap.CompressFormat format) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    if (format == null) {
        format = Bitmap.CompressFormat.PNG;
    }/*from   w  ww.jav a  2 s.  co m*/

    bitmap.compress(format, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, String fileName) {
    String filePath = Environment.getExternalStorageDirectory() + "/" + fileName;
    try {//from   w ww.  j  ava 2  s  . co  m
        OutputStream stream = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
        stream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static File bitmapToFile(Context context, Bitmap bitmap) {
    File outputFile = getTempFile(context);
    FileOutputStream fos = null;/*from   ww w  .  j  av a2  s.  com*/
    try {
        fos = new FileOutputStream(outputFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }

    return outputFile;
}

From source file:Main.java

public static @Nullable byte[] toByteArray(@Nullable Bitmap bitmap) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bm) {
    if (bm == null || bm.isRecycled()) {
        return null;
    }//from  w w  w.  ja  v  a  2  s.  c  om
    byte[] bytes;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    bytes = baos.toByteArray();
    try {
        baos.flush();
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:Main.java

public static boolean saveBitmap(File file, Bitmap bitmap) {
    if (file == null || bitmap == null)
        return false;
    try {/*from   www.j  a v  a2s .c o  m*/
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        return bitmap.compress(CompressFormat.JPEG, 100, out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static String bitmapToString(String filePath) {

    Bitmap bm = getSmallBitmap(filePath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.JPEG, 40, baos);
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] toByteArray(final Bitmap bmp, boolean recycle) {
    byte[] result = null;
    if (bmp != null) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 85, output);
        result = output.toByteArray();/*from w  w  w  .j  ava2  s  .c o  m*/
        if (recycle) {
            bmp.recycle();
        }
    }
    return result;
}