public Bitmap decodeSampledBitmapFromIs(InputStream is,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
Log.i("decodeFromIs: ", "width " + reqWidth + " height " + reqHeight + " inSampleSize : " + options.inSampleSize);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeStream(is, null, options);
return b;
}
위와 같은 소스가 있습니다.
출처는
여기 구요!
출처에선 Resource로 하였으나 저는 외부에서 받아오는 이미지라서 InputStream으로 재구현하였습니다.
위 함수의 과정을 요약하면
이미지 크기 정보 받아오기 -> 이미지 크기에 따른 InSampleSize 값 -> 정해진 옵션으로 이미지 디코드 -> 비트맵 반환
입니다.
근데 이미지가 안떠요.. 아래 두줄을 지우면 잘 뜹니다.
options.inJustDecodeBounds = true; //이미지를 decode할 때 이미지의크기정보만 가져와서 OOM이 발생하지 않음.
BitmapFactory.decodeStream(is, null, options); //이 과정에서 options.width와height가 구해집니다.
하지만 저걸 지우면 유동적인 InSampleSize를 구할수 없어집니다...ㅠㅠㅠㅠㅠㅠ
원인이 뭘까요 왜, 저 두줄을 지우면 잘될까요,,
InputStream의 어떠한 특성이 있는건지..... 삽질끝에 질문올립니다 ㅠ