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.decodeStream
null입니다. 이전 Android API에서 올바르게 작동합니다.
디버그 모드에서 다음 메시지가 표시됩니다.
09-04 10 : 10 : 50.384 6274-6610 / myapp D / skia : --- SkAndroidCodec :: NewFromStream이 null을 반환했습니다.
누군가가 코딩 이유와 수정 방법을 말해 줄 수 있습니까?
편집 : 한편 inJustDecodeBounds = true로 첫 번째 BitmapFactory.decodeStream을 제거하면 나중에 inJustDecodeBounds = false로 성공적인 BitmapFactory.decodeStream이 발생한다는 것을 발견했습니다. 이유를 모르고 비트 맵 크기의 측정 값을 대체하는 방법을 모릅니다.
나는 우리가 같은 배에 있다고 생각합니다. 우리 팀은 당신처럼 잠시 동안이 문제에 붙어 있습니다.
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] 삭제
몇 마디 만하겠습니다