我有一个来自assets / drawable-nopdpi文件夹的相当大的图片。 图像大小为1173x1285,497KB。 它没有加载..
的onCreate
AssetManager assetManager = this.getAssets();
Bitmap bitmap = null;
InputStream is = null;
try {
is = assetManager.open("indoormapimg.png");
bitmap = decodeSampledBitmapFromResource(is, 100, 100); //trying 100x100
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("tag", String.valueOf("Bitmap: " + bitmap));
方法
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)
if (width > height)
inSampleSize = Math.round((float)height / (float)reqHeight);
else
inSampleSize = Math.round((float)width / (float)reqWidth);
return inSampleSize;
}
和
public static Bitmap decodeSampledBitmapFromResource(InputStream is, int reqWidth, int reqHeight) {
// 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, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(is, null, options);
}
我也尝试过使用Resources。徒劳无功..
我也尝试过使用inJustBounds
的true和false的BitmapFactory。还有inScaled
我似乎无法加载任何图像,我的日志总是返回null ..
编辑:
我也在尝试
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), ResID);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inScaled = false;
Bitmap bitmapImage = BitmapFactory.decodeResource(this.getResources(), ResID, opts);
Log.d("tag", String.valueOf("Bitmap: " + bitmap + " BitmapImage: " + bitmapImage));
返回Bitmap: null BitmapImage: null
此外,LogCat显示--- decoder->decode returned false
答案 0 :(得分:0)
事实证明,我正在解码的文件已损坏。我不得不改变原始文件。谢谢。