android 7的系统上BitmapFactory.decodeStream函数第二次调用的时候会报错SkAndroidCodec::NewFromStream returned null,这是因为Android 7.0对BitmapFactory修改了BitmapFactory代码。
解决方法就是在两次decodeStream之间reset一下InputStream,增加如下的代码。
try {
is.reset();
} catch (IOException e) {
return null;
}
于是,读取bitmap代码如下
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);
try { is.reset(); } catch (IOException e) { }// 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);}
来源:
https://stackoverflow.com/questions/39316069/bitmapfactory-decodestream-from-assets-returns-null-on-android-7