Вы должны взглянуть на учебные документы для Managing Bitmap Memory. В зависимости от вашей версии ОС вы можете использовать разные методы, чтобы позволить вам управлять большим количеством растровых изображений, но вам, вероятно, придется менять свой код.
В частности, вы, вероятно, придется использовать измененную версию кода в «Load a Scaled Down Version into Memory», но я по крайней мере, нашел этот раздел особенно полезным:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Этот Способ позволяет легко загружать растровое изображение произвольно большого размера в ImageView, который отображает 100x100 пикселей, эскиз, как показано на следующий пример кода:
mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
Я не согласен с обозначением этого как дубликата. В этом вопросе одно и то же изображение повторно загружается в цикл. Даже если изображение будет очень маленьким, вы получите ошибку из памяти, если количество итераций достаточно велико. – britzl