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 byte[] InputStreamToByte(InputStream is) throws IOException {
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from  w ww  .  j a  v  a  2  s  . c o m*/
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte imgdata[] = bytestream.toByteArray();
    bytestream.close();

    return imgdata;
}

From source file:Main.java

public static String encodeToBase64(Bitmap image) {
    Bitmap immagex = image;/*from   w ww.j  a v a2 s .c  om*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

public static InputStream Bitmap2IS(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 70, baos);
    InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
    return sbs;//from   www .ja  v  a 2s.c  o  m
}

From source file:Main.java

public static String convertBitmapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    byte[] b = baos.toByteArray();
    String temp = null;/*from   w  ww.j a  v a  2 s  .co  m*/
    try {
        System.gc();
        temp = Base64.encodeToString(b, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        b = baos.toByteArray();
        temp = Base64.encodeToString(b, Base64.DEFAULT);
    }
    return temp;
}

From source file:Main.java

private static byte[] bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from w w w. j a va2  s.  co m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static String getBase64FromBitmap(Bitmap bitmap, int bitmapQuality) {
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, bitmapQuality, bStream);
    byte[] bytes = bStream.toByteArray();
    return Base64.encodeToString(bytes, Base64.DEFAULT);
}

From source file:Main.java

public static String file2String(File file) {
    FileInputStream fis = null;/*  ww  w.j ava  2 s.  c o  m*/
    ByteArrayOutputStream baos = null;
    try {
        fis = new FileInputStream(file);
        baos = new ByteArrayOutputStream();
        int i;
        while ((i = fis.read()) != -1) {
            baos.write(i);
        }
        String str = baos.toString();
        return str;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static byte[] inputStreamToByte(InputStream in) throws IOException {

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] data = new byte[BUFFER_SIZE];
    int count = -1;
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
        outStream.write(data, 0, count);

    data = null;/*from   w w  w .  ja  v a  2s .c  o m*/
    return outStream.toByteArray();
}

From source file:Main.java

public static byte[] getImageByte(Bitmap bitmap) {
    // imageView.setDrawingCacheEnabled(true);
    // Bitmap bitMap = imageView.getDrawingCache();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    return imageBytes;
}

From source file:Main.java

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