마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 스튜디오에서 서버 전송을 위해 bitmap에서 io.file로 어떻게 바꾸나요?

0 추천

서버에 이미지를 올리는데 용량을 줄이기 위해 bitmap에서 decodeFile을 이용해서 용량을 줄이긴 했는데 이 이미지를 서버로 올리려고 io.file로 바꾸려고 하는데 어떻게 해야할지를 모르겠습니다.

일단 구글링을 해보는데까지 해서 io.file로 전환 후 전송은 했는데 자세한 설명이 안되어있어 원리를 이해하기 힘들었고 중요한건 원본 사진의 메타데이터가 손상되어서 적용시키기 힘들게 됐습니다.

//이미지 용량 줄이기
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap orgImage = BitmapFactory.decodeFile(path, options);

File file = new File(path);//원본 이미지의 경로가 들어있습니다.
try {
         OutputStream os = new BufferedOutputStream(new   FileOutputStream(file));
         orgImage.compress(Bitmap.CompressFormat.JPEG, 100, os);
         os.close();
}catch(IOException e){
        e.printStackTrace();
}

이미지 용량을 줄이는 코드와 io.file로 바꾸는 코드는 위와 같이 작성했는데 어떻게 수정해야 하나요?

익명사용자 님이 2019년 5월 6일 질문

1개의 답변

0 추천
굳이 file로 만들 필요 없습니다.

아래와 같이 해 주시면 될 듯 하네요.

 

1.  사이즈 줄임
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap orgImage = BitmapFactory.decodeFile(path, options);

 
2.  bitmap 값을 byteArray 로 읽어 들임.
ByteArrayOutputStream stream = new ByteArrayOutputStream() ;  
orgImage.compress( CompressFormat.JPEG, 100, stream) ;  
byte[] byteArray = stream.toByteArray() ;  

3. byteArray 데이터를 Post 형식으로 서버에 전송  
HttpPost httpPost = new HttpPost("전송할 주소");
httpPost.setEntity(new ByteArrayEntity(byteArray));           
HttpResponse response = httpClient.execute(httpPost);
익명사용자 님이 2019년 5월 7일 답변
...