주로, 서버에 있는 사진을 Http 통신으로 가져와서 Bitmap을 Return 하는 경우도 있지만, 바로 바이너리로 써야할 경우도 있다.
아래는 InputStream 을 Byte 배열로 Return 하는 함수이다.
서버가 보안이 걸려있어서, 외부망에서는 사진을 못가지고 왔다 ㅠ 그래서 결국 바이너리를 내려받기로 했다. 휴-



private static byte[] readFromStream(InputStream pInputStream) {
        if (pInputStream == null) {
            return null;
        }

        int lBufferSize = 1024;
        byte[] lByteBuffer = new byte[lBufferSize];

        int lBytesRead = 0;
        int lTotbytesRead = 0;
        int lCount = 0;

        ByteArrayOutputStream lByteArrayOutputStream = new ByteArrayOutputStream(lBufferSize * 2);

        try {
            while ((lBytesRead = pInputStream.read(lByteBuffer)) != -1) {
                lTotbytesRead += lBytesRead;
                lCount++;

                lByteArrayOutputStream.write(lByteBuffer, 0, lBytesRead);
            }
        } catch (Throwable e) {
            e.printStackTrace(System.out);
        }

        byte[] lDataBytes = lByteArrayOutputStream.toByteArray();

        return lDataBytes;
    }

'안드로이드' 카테고리의 다른 글

Fragment onCreateView inflate Exception  (0) 2014.12.26
TextView Font Size 코드  (0) 2014.11.06
Bitmap Round  (0) 2014.06.05
ViewPager  (0) 2014.05.15
DrawerLayout  (0) 2014.05.15
Posted by 자바리즘
,