Sav*_*ari 5 android android-assets bitmapfactory android-7.0-nougat
如何从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);
}
Run Code Online (Sandbox Code Playgroud)
因此(仅限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.不知道原因并且不知道如何替换位图大小的测量.
Ake*_*ist 10
我想我们在同一条船上.我的团队像你一样坚持了这个问题.
这似乎是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");
}
Run Code Online (Sandbox Code Playgroud)
我发现该BitmapFactory.decodeStream方法在我们设置之后没有创建位图,inJustDecodeBounds=false但是当我尝试创建没有绑定解码的位图时.其作品!问题是关于BitmapOptions,因为当我们BitmapFactory.decodeStream再次调用时,InputStream不会更新.
所以我再次解码之前重置该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;
}
Run Code Online (Sandbox Code Playgroud)
看起来我们必须在每次重用之前重置InputStream.
| 归档时间: |
|
| 查看次数: |
9613 次 |
| 最近记录: |