Example usage for java.io ByteArrayOutputStream toByteArray

List of usage examples for java.io ByteArrayOutputStream toByteArray

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream toByteArray.

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Creates a newly allocated byte array.

Usage

From source file:Main.java

public static String convertToBase64(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] byteArray = stream.toByteArray();
    try {/*w ww .  ja v  a 2  s . c  o  m*/
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] convertBitmapToByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

From source file:Main.java

private static byte[] load(File file) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    copy(new FileInputStream(file), baos);
    return baos.toByteArray();
}

From source file:Main.java

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

From source file:Main.java

private static byte[] bitmap2Bytes(Bitmap bm) {
    try {/*w w  w . ja v  a 2s.  c o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Bitmap into compressed PNG//from   w  ww  . j  a  v a  2  s  .c o  m
 *
 * @param image the Bitmap
 * @return byte array of PNG
 */
public static byte[] bitmapToPng(final Bitmap image) {
    if (image == null)
        return null;

    ByteArrayOutputStream ba = new ByteArrayOutputStream();

    if (image.compress(CompressFormat.PNG, 100, ba))
        return ba.toByteArray();
    else
        return null;
}

From source file:Main.java

/**
 * convert Bitmap to byte array/*from   w  w w. ja va2 s .c om*/
 *
 * @param b
 * @return
 */
public static byte[] bitmapToByte(Bitmap b) {
    if (b == null) {
        return null;
    }

    ByteArrayOutputStream o = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG, 100, o);
    return o.toByteArray();
}

From source file:Main.java

public static byte[] toBytes(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, PNG_SAVE_QUALITY, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
    byte[] b = baos.toByteArray();
    return Base64.encodeToString(b, Base64.DEFAULT);
}