2013-10-21 58 views
-1

我有九张图片需要在一个活动开始时加载,并且出现OutOfMemory异常问题。起初我直接在xml设置它的src中加载​​它们。因此,越来越java.lang.OutOfMemory后,我意识到,也许我需要更有效地加载照片,我创造了这个循环在活动开始时要执行: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)); 
} 

但我仍然有同样的问题,内存不足,关于我在做什么错误的任何想法? googeling的

+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; 
+0

谢谢,这为我工作,但不删除'options.inJustDecodeBounds = false;'因为否则图像不加载。 – Monica