옵션이 설정되면 BitmapFactory.decodeStream이 null을 반환

로버트 포스 :

에 문제가 BitmapFactory.decodeStream(inputStream)있습니다. 옵션없이 사용하면 이미지가 반환됩니다. 그러나 옵션을 옵션과 함께 사용하면 .decodeStream(inputStream, null, options)비트 맵이 반환되지 않습니다.

내가하려고하는 것은 실제로 메모리를 절약하기 위해 비트 맵을로드하기 전에 비트 맵을 다운 샘플링하는 것입니다. 나는 좋은 가이드를 읽었지만 아무도 사용하지 않습니다 .decodeStream.

잘 작동합니다

URL url = new URL(sUrl);
HttpURLConnection connection  = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);

작동하지 않습니다

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);

InputStream is = connection.getInputStream();

Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeStream(is, null, options);

Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);

if (options.outHeight * options.outWidth * 2 >= 200*100*2){
    // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
    double sampleSize = scaleByHeight
    ? options.outHeight / TARGET_HEIGHT
    : options.outWidth / TARGET_WIDTH;
    options.inSampleSize =
        (int)Math.pow(2d, Math.floor(
        Math.log(sampleSize)/Math.log(2d)));
}

// Do the actual decoding
options.inJustDecodeBounds = false;
Bitmap img = BitmapFactory.decodeStream(is, null, options);
로버트 포스 :

문제는 HttpUrlConnection에서 InputStream을 사용하여 이미지 메타 데이터를 가져 오면 동일한 InputStream을 되 감고 다시 사용할 수 없다는 것입니다.

따라서 이미지의 실제 샘플링을 위해 새 InputStream을 만들어야합니다.

  Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;

  BitmapFactory.decodeStream(is, null, options);

  Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);

  if(options.outHeight * options.outWidth * 2 >= 200*200*2){
         // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
        double sampleSize = scaleByHeight
              ? options.outHeight / TARGET_HEIGHT
              : options.outWidth / TARGET_WIDTH;
        options.inSampleSize = 
              (int)Math.pow(2d, Math.floor(
              Math.log(sampleSize)/Math.log(2d)));
     }

        // Do the actual decoding
        options.inJustDecodeBounds = false;

        is.close();
        is = getHTTPConnectionInputStream(sUrl);
        Bitmap img = BitmapFactory.decodeStream(is, null, options);
        is.close();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Assets의 BitmapFactory.decodeStream이 Android 7에서 null을 반환합니다.

Android 스튜디오, Kotlin : BitmapFactory.decodeStream ()이 null을 반환합니다.

사용자가 선택한 이미지의 스트림을 가져 오는 방법 (BitmapFactory.DecodeStream (inputStream)이 null을 반환 함)

BitmapFactory는 이미지가 있지만 null을 반환합니다.

파일 경로에서 ImageView의 이미지 설정. BitmapFactory.decodeFile, BitmapFactory.decodeStream 또는 Drawable.fromFile?

BitmapFactory.decodeByteArray가 null을 반환하는 이유는 무엇입니까?

내 BitmapFactory.decodeByteArray가 null을 반환하는 이유는 무엇입니까?

옵션 값을 가져 오면 항상 'null'이 반환됩니다.

json이 null을 반환하면 '0'으로 설정

Android 7에서 Assets의 BitmapFactory.decodeStream이 때때로 실패 함

BitmapFactory.decodeStream이 작동하지 않습니다.

위치 설정 요청이 거부되면보기를 설정하면 null 포인터가 반환됩니까?

null Bitmap을 반환하는 BitmapFactory

BitmapFactory.decodeStream returns null (Image in jar file)

옵션이 전달되면 argparse의 변수로 값을 설정하십시오.

행이 없으면 NULL을 반환 SQL

Draggable이 취소되면 반환되지 않도록 설정

arping과 함께 스푸핑 된 IP 옵션을 사용하면 응답이 반환되지 않습니다.

값을 비우면 원래 필터링된 옵션이 반환되지 않습니다.

BitmapFactory.decodeFile은 inJustDecodeBounds가 false로 설정된 상태에서 null을 반환합니다.

설정 양식에 기본값을 설정하면 Octobercms에서 null이 반환됩니다.

선택 옵션 속성이 JQuery로 정의되지 않음을 반환합니다.

값이 설정되면 Laravel 모델이 null이 아닌 위반을 생성합니다.

Moq 설정이 null을 반환합니다.

높이가 100 %로 설정되면 스크롤 위치가 0을 반환합니다.

반응 형에서 빈 문자열을 null로 설정하면 빈 문자열이되는 이유

반환 옵션 반환 된 옵션 값이있는 경우 그대로, 다른 호출을 다른 함수

세션 시간이 초과되면 "HttpContext.Current.User.Identity.Name"이 null을 반환합니까?

아무것도 선택되지 않은 경우 jQuery option : selected가 첫 번째 옵션을 반환하는 이유

TOP 리스트

뜨겁다태그

보관