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 InputStream bitmap2InputStream(Bitmap bm, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/*from  w  w w.jav  a  2 s.c o  m*/
}

From source file:Main.java

public static byte[] toByteArray(final Bitmap bitmap) {

    final ByteArrayOutputStream output = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);

    bitmap.recycle();//w  w  w  .  j  av a 2s.co  m

    return output.toByteArray();
}

From source file:Main.java

public static String getBase64FromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

public static String base64Encode(ImageView imageView) {
    if (imageView.getDrawable() != null) {
        Bitmap avatarBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        avatarBitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);
        byte[] avatarByteArray = baos.toByteArray();
        return Base64.encodeToString(avatarByteArray, Base64.DEFAULT);
    } else {//www.j av  a2s .c  om
        return null;
    }
}

From source file:Main.java

public static byte[] BitmapToBytes(Bitmap paramBitmap) {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    paramBitmap.compress(Bitmap.CompressFormat.PNG, 100, localByteArrayOutputStream);
    return localByteArrayOutputStream.toByteArray();
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] getBitmapBytes(Bitmap paramBitmap) {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    paramBitmap.compress(Bitmap.CompressFormat.PNG, 100, localByteArrayOutputStream);
    return localByteArrayOutputStream.toByteArray();
}

From source file:Main.java

/**
 * Write image to internal storage/*  w  ww.j av a 2s . c  o  m*/
 * @param context  : Context android.
 * @param fileName : Image file name.
 * @param image    : Bitmap image format PNG only.
 */
public static void saveImageToInternalStorage(Context context, String fileName, Bitmap image) {
    // Convert Bitmap to byteArray
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    // Open fileOutput with fileName and write byteArray
    try {
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        if (fos != null) {
            fos.write(byteArray);
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(byteArray, 0);
}

From source file:Main.java

/**
 * Method to convert a Bitmap into a byte[].
 * Attention: this method is very expensive!!!
 *
 * @param source The Bitmap to convert.//from  w  w w  .  ja va  2 s  . c o m
 * @return The byte[] that represents the source Bitmap.
 */
public static byte[] toByteArray(Bitmap source) throws IOException {
    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    source.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
    byte[] photoByteArray = blob.toByteArray();
    blob.close();
    return photoByteArray;
}