【发布时间】:2017-01-11 23:12:40
【问题描述】:
如何在 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 为空。它可以在较旧的 Android API 上正常工作。
在调试模式下,我看到以下消息:
09-04 10:10:50.384 6274-6610/myapp D/skia: --- SkAndroidCodec::NewFromStream 返回 null
谁能告诉我原因以及如何更正编码?
编辑:同时我发现,使用 inJustDecodeBounds=true 删除第一个 BitmapFactory.decodeStream 会导致成功的 BitmapFactory.decodeStream 之后使用 inJustDecodeBounds=false。不知道原因,也不知道如何替换位图大小的测量。
【问题讨论】:
-
我的猜测是您的问题是特定于您的资产的。我刚刚在运行 Android 7.0 的 Nexus 9 上测试了从资产加载图像的 one of my book samples。它似乎工作正常。
标签: android bitmapfactory android-assets android-7.0-nougat