read InputStream to byte array - Android java.io

Android examples for java.io:InputStream

Description

read InputStream to byte array

Demo Code

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Main{

    public static byte[] readStream(InputStream inStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }//from   w  ww .  j a  va2 s  .c  om
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;

    }

}

Related Tutorials