Code example for ByteArrayOutputStream

Methods: closetoByteArrayflush

0
 
    /** 
     * Helper method to compress the given bitmap as a JPEG and return the resulting byte array. 
     */ 
    private byte[] getCompressedBytes(Bitmap b, int quality) throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final boolean compressed = b.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        baos.flush();
        baos.close();
        byte[] result = baos.toByteArray();
 
        if (!compressed) {
            throw new IOException("Unable to compress image");
        } 
        return result;
    } 
 
    /** 
     * Retrieves the uncompressed display photo. 
     */