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

사 바리

Android 7의 Asset 디렉토리에서 비트 맵을 디코딩하는 방법은 무엇입니까?

내 앱은 Marshmallow까지 Android 버전에서 잘 실행됩니다. Android 7에서는 Asset 디렉토리에서 이미지를로드하지 못합니다.

내 코드 :

private Bitmap getImage(String imagename) {
    // Log.dd(logger, "AsyncImageLoader: " + ORDNER_IMAGES + imagename);

    AssetManager asset = context.getAssets();
    InputStream is = null;
    try {
        is = asset.open(ORDNER_IMAGES + imagename);
    } catch (IOException e) {
        // Log.de(logger, "image konnte nicht gelesen werden: " + ORDNER_IMAGES + imagename);
        return null;
    }


    // 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, PW, PH);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    // Lesen des Bitmaps in der optimierten Groesse
    return BitmapFactory.decodeStream(is, null, options);

}

결과적으로 (Android 7 만 해당) BitmapFactory.decodeStreamnull입니다. 이전 Android API에서 올바르게 작동합니다.

디버그 모드에서 다음 메시지가 표시됩니다.

09-04 10 : 10 : 50.384 6274-6610 / myapp D / skia : --- SkAndroidCodec :: NewFromStream이 null을 반환했습니다.

누군가가 코딩 이유와 수정 방법을 말해 줄 수 있습니까?

편집 : 한편 inJustDecodeBounds = true로 첫 번째 BitmapFactory.decodeStream을 제거하면 나중에 inJustDecodeBounds = false로 성공적인 BitmapFactory.decodeStream이 발생한다는 것을 발견했습니다. 이유를 모르고 비트 맵 크기의 측정 값을 대체하는 방법을 모릅니다.

Akexorcist

나는 우리가 같은 배에 있다고 생각합니다. 우리 팀은 당신처럼 잠시 동안이 문제에 붙어 있습니다.

BitmapFactory.cpp ( https://android.googlesource.com/platform/frameworks/base.git/+/master/core/jni/android/graphics/BitmapFactory.cpp ) 일부 코드가 Android에 추가 된 것 같습니다. 7.0 문제가 발생했습니다.

// Create the codec.
NinePatchPeeker peeker;
std::unique_ptr<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(streamDeleter.release(), &peeker));
if (!codec.get()) {
    return nullObjectReturn("SkAndroidCodec::NewFromStream returned null");
}

그리고 BitmapFactory.decodeStream우리가 설정 한 후에는 방법이 비트 맵 inJustDecodeBounds=false을 만들지 않았지만 바인딩 된 디코딩없이 비트 맵을 만들려고 할 때 발견했습니다 . 작동합니다! 문제는 InputStream이 BitmapFactory.decodeStream다시 호출 할 때 업데이트되지 않는다는 BitmapOptions에 관한 것입니다 .

그래서 다시 디코딩하기 전에 InputStream을 재설정했습니다.

private Bitmap getBitmapFromAssets(Context context, String fileName, int width, int height) {
    AssetManager asset = context.getAssets();
    InputStream is;
    try {
        is = asset.open(fileName);
    } catch (IOException e) {
        return null;
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);
    try {
        is.reset();
    } catch (IOException e) {
        return null;
    }
    options.inSampleSize = calculateInSampleSize(options, width, height);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(is, null, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

재사용하기 전에 매번 InputStream을 재설정해야하는 것 같습니다.

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

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

BitmapFactory.decodeResource는 Android 2.2에서 변경 가능한 비트 맵을 반환하고 Android 1.6에서 변경 불가능한 비트 맵을 반환합니다.

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

JSON의 LinkedHashMap이 get ()에서 null을 반환합니다.

Android SwipeRefreshLayout이 null을 반환합니다.

Android getParentFragment ()는 Fragment 내의 ViewPager에서 null을 반환합니다.

SupportMapFragment의 findFragmentById가 Android Studio에서 null을 반환합니다.

Android getWidth ()는 View의 onDraw에서 0을 반환합니다.

CASE 문에서 임의로 생성 된 값이 NULL을 반환합니다.

listFiles ()는 Android 6.0 에뮬레이터에서 null을 반환합니다.

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

Android DataBinding setVariable () 다음에 getVariable () 호출이 null을 반환합니다.

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

MainActivity의 OnCreate에서 FindViewById <Android.Support.V7.Widget.Toolbar> (Resource.Id.toolbar)가 null을 반환합니다.

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

Android BitmapFactory.decodeFile (path)는 항상 null을 반환합니다.

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

NSString에서 NSDate 로의 변환이 null을 반환합니다.

Nexus 7을 사용하는 카메라의 Android 동영상 기록은 null 데이터를 반환합니다.

Android : findViewById는 setContentView 이후에도 Null을 반환합니다.

BitmapFactory.decodeResource는 Android 2.2에서 null을 제공합니다.

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

조각이있는 Android 에스프레소. getActivity가 NULL을 반환합니다.

BitmapFactory.decodeByteArray는 Android에서 inJustDecoteBounds = true로 null을 반환합니다.

위치 관리자는 iOS의이란에서 null 값을 반환합니다.

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

Android camera.open (0)은 Galaxy s7에서 null을 반환합니다.

서버의 JSON 응답 일부가 null을 반환합니다. Android

BitmapFactory.decodeFile ()은 일부 장치에서 null을 반환합니다.