我正在創建自己的SlideShow活動,其中有一個ImageView,每當用戶單擊下一個或上一個時,我都會用適當的圖像填充它。幻燈片放映中的OutOfMemory
這些圖片雖然很大,但只要我瀏覽其中的幾個圖片,我就會得到一個OutofMemory,可能是由於我的內存泄漏。
問題是,每次調用一個新圖像時,我都會嘗試清空以前的位圖,因此它被垃圾收集,但無濟於事。
我在這裏錯過了什麼嗎?
我附上我的活動非常簡單的代碼:
public class SlideShowActivity extends Activity {
private static final String LOG_TAG = SlideShowActivity.class.getSimpleName();
private Bitmap currentBitmap;
private Bitmap mDefaultBitmap;
private ImageView imageView;
private ImageButton buttonPrev;
private ImageButton buttonNext;
private int currentImageIndex;
private String imguri[] = { "ruta1/imagen1.jpg", "ruta1/imagen2.jpg", "ruta1/imagen3.jpg", "ruta1/imagen4.jpg", "ruta1/imagen5.jpg", "ruta1/imagen6.jpg" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
mDefaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.noimage);
imageView = (ImageView) findViewById(R.id.imageViewCurrentImage);
buttonPrev = (ImageButton) findViewById(R.id.imgbuttonprev);
buttonNext = (ImageButton) findViewById(R.id.imgbuttonnext);
currentImageIndex = 0;
setCurrentImage(currentImageIndex);
buttonPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
currentImageIndex--;
if(currentImageIndex < 0)
currentImageIndex = imguri.length-1;
setCurrentImage(currentImageIndex);
}
});
buttonNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
currentImageIndex++;
if(currentImageIndex == imguri.length)
currentImageIndex = 0;
setCurrentImage(currentImageIndex);
}
});
}
private void setCurrentImage(int idx) {
currentBitmap = null;
imageView.setImageBitmap(null);
try {
currentBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(getAssets().open(imguri[idx])),800, 480, true);
imageView.setImageBitmap(currentBitmap);
} catch (IOException e) {
Log.d(LOG_TAG, "No se pudo abrir el recurso" + imguri[0]);
imageView.setImageBitmap(mDefaultBitmap);
}
}
}
圖像尺寸大於告訴你..所以首先計算你的圖像的大小,然後存儲到creatbitmapscale然後使用它....去吧? – QuokMoon
不是我在做的事情嗎? –