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 byte[] bitmapToByteArray(Bitmap bitmap, int imageQuality) {
    ByteArrayOutputStream bos = null;
    try {/*ww w  . j  a v  a2 s .  c om*/
        bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, imageQuality, bos);
        return bos.toByteArray();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                Log.w(TAG, "Failed to close ByteArrayOutputStream", e);
                // Ignore exception
            }
        }
    }
}

From source file:Main.java

public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    } else {/* w  ww  .  j av a  2  s. co m*/
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }
}

From source file:Main.java

public static String saveToPrivateFile(Activity activity, String filename, Bitmap bmp) {
    FileOutputStream fos;// w  w  w  . java2 s. c o  m
    try {
        fos = activity.openFileOutput(filename, Context.MODE_PRIVATE);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();

        return filename;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }

}

From source file:Main.java

public static boolean saveToFile(Bitmap bitmap, File file) {
    FileOutputStream outputStream = null;
    try {//from   w ww .java  2 s. co m
        outputStream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, outputStream);
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            outputStream.close();
        } catch (Exception e) {
            // nothing
        }
    }
}

From source file:Main.java

public static byte[] bitmapToByte(Bitmap bitmap, Bitmap.CompressFormat fmt) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(fmt, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static void saveJPGE_After(Bitmap bitmap, String path) {
    File file = new File(path);
    try {//from ww  w.ja v  a 2s . c o  m
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@Deprecated
public static void writeBitmapAttribute(XmlSerializer out, String name, Bitmap value) throws IOException {
    if (value != null) {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        value.compress(CompressFormat.PNG, 90, os);
        writeByteArrayAttribute(out, name, os.toByteArray());
    }//  ww w. j  a  v  a 2  s  . c om
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }// ww  w .j a v  a  2  s.com
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static void savePNG_After(Bitmap bitmap, String name) {
    File file = new File(name);
    try {//  w  w w  .  j av a2s .c  om
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] decodeBytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//  w w  w. j a v  a 2 s  .  c o  m
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    } catch (OutOfMemoryError e) {
        return null;
    }
}