Code example for ByteArrayOutputStream

Methods: close, flush, toByteArray

0
     * Creates a byte[] containing the PNG-compressed bitmap, or null if 
     * something goes wrong. 
     */ 
    public static byte[] compressBitmap(Bitmap bitmap) {
        final int size = bitmap.getWidth() * bitmap.getHeight() * 4;
        final ByteArrayOutputStream out = new ByteArrayOutputStream(size);
        try { 
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
            Log.w(TAG, "Unable to serialize photo: " + e.toString());
            return null; 
        } 
    } 
 
    /** 
     * Adds common extras to gallery intents. 
     * 
     * @param intent The intent to add extras to.