Convert InputStream to Byte array - Android File Input Output

Android examples for File Input Output:Byte Array Convert

Description

Convert InputStream to Byte array

Demo Code


//package com.java2s;

import java.io.InputStream;

public class Main {

    public static byte[] inputStream2Bytes(InputStream is) {
        String str = "";

        byte[] readByte = new byte[1024];

        @SuppressWarnings("unused")
        int readCount = -1;

        try {/*from  w  w w.  j a va2 s  .c  o  m*/
            while ((readCount = is.read(readByte, 0, 1024)) != -1) {
                str += new String(readByte).trim();
            }

            return str.getBytes();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related Tutorials