我想在活动开始时加载九张图片,而且我遇到了OutOfMemory异常的问题。起初我将它们直接加载到xrc设置其src中。在获得java.lang.OutOfMemory后,我意识到我可能需要更有效地加载图片,并且我创建了这个循环,以便在活动开始时执行:
for(int i=0;i<9;i++){
String background = "background"+(i+1);
int idDrawable = getResources().getIdentifier(background, "drawable", getPackageName());
int idPicture = getResources().getIdentifier(background, "id", getPackageName());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), idDrawable, options);
options.inJustDecodeBounds = false;
ImageView image = (ImageView) findViewById(idPicture);
image.setImageBitmap(BitmapFactory.decodeResource(getResources(), idDrawable, options));
}
但我仍然有同样的OutOfMemory问题,关于我做错了什么想法?
答案 0 :(得分:2)
使用以下代码。 更改此代码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), idDrawable, options);
到
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 4;
BitmapFactory.decodeResource(getResources(), idDrawable, options);
并删除此行
options.inJustDecodeBounds = false;
答案 1 :(得分:0)
一点点googeling和自己搜索会引导你进入Android开发者HowTo
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html