Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

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

Prototype

public ByteArrayOutputStream() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:Main.java

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

From source file:Main.java

public static byte[] decodeBitmapToBytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (bitmap != null) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    } else {//from w w  w .ja v a2s. com
        Log.e(TAG, "bitmap is null");
        return null;
    }
}

From source file:Main.java

public static String getStringFromInputStream(InputStream is) {
    if (is == null) {
        return null;
    }//  w w  w  .  ja  v a  2 s.  c o  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[4 * 1024];
    int len = 0;
    try {
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
    } catch (Exception e) {
    }
    return baos.toString();
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, output);
    if (needRecycle) {
        bmp.recycle();//from   w w w .j ava  2 s.  com
    }
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * Converts a Bitmap to a byteArray./*from ww  w  .j av  a2  s.  co  m*/
 * @return byteArray
 */
public static byte[] bitmapToByteArray(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    } else {//from  w  w  w .  j ava  2s.c  om
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }
}

From source file:Main.java

public static byte[] compressStringToByteArray(String uncompressedString) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream;
    try {// w  ww .  j  a  va  2 s .  c  o  m
        gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(uncompressedString.getBytes("UTF-8"));
        gzipOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

public static InputStream getInputStreamFromBitmap(Bitmap bitmap) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    final byte[] bitmapdata = out.toByteArray();
    final ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
    return bs;/*from   w w  w. j av  a  2  s. co m*/
}

From source file:Main.java

public static String readStream(InputStream is) {
    try {//  ww w.  j a va 2 s .  c o m
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int i = is.read();
        while (i != -1) {
            bo.write(i);
            i = is.read();
        }
        return bo.toString();
    } catch (IOException e) {
        return "";
    }
}

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);
}