我有3個ImageViews,我從網址隨機設置圖像。我得到java.lang.OutOfMemoryError。我正在使用以下代碼爲ImageView設置圖像。如何擺脫java.lang.OutOfMemoryError?
public void changeImages(){
try {
for(int i=0; i<items.size(); i++){
URL imageUrl = new URL(items.get(i).getImageUrl());
if(i == 0){
image1.setImageBitmap(BitmapFactory.decodeStream(imageUrl.openStream()));
}else if(i == 1){
image2.setImageBitmap(BitmapFactory.decodeStream(imageUrl.openStream()));
}else if(i == 2){
image3.setImageBitmap(BitmapFactory.decodeStream(imageUrl.openStream()));
}
}
} catch (Exception ex) {
System.out.println("Exception in parseResponse: "+ex);
}
}
image1,image2和image3是ImageViews。幾次改變圖像後,我得到java.lang.OutOfMemoryError。任何想法我應該如何擺脫它?提前致謝。
在谷歌查找內存泄漏。別客氣。 (但是,如果你改變了,因爲你沒有關閉你在你的循環中打開的'imageUrl'流,所以你的圖片會嚴格保留在後面) –
有很好的庫來處理下載和顯示圖像到imageview中,在google中搜索一些。 – hardartcore