Java tutorial
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.InputStream; public class Main { /** * Converts an inputstream to a byte array (Mostly useful for sending images via JSON) * @param is Input stream, if using a URI, open it by calling: * InputStream iStream = context.getContentResolver().openInputStream(uri); * @return Byte Array */ public static byte[] getBytes(InputStream is) { try { ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = is.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } return byteBuffer.toByteArray(); } catch (Exception e) { return null; } } }