J'ai une fragmentActivity qui a un tabHost. Chaque onglet a à l'intérieur un fragment qui montre à l'écran un ButtonTable. Le code suivant est le ButtonTable:Android. Pourquoi est-ce que j'obtiens une erreur OutOfMemory lors du chargement de BitMap?
public class ButtonTable extends Button {
private Paint paint= new Paint();
private Bitmap bmp= null;
private Table table= null;
int width= 0;
int height= 0;
boolean adjustmentForm= false;
private WeakReference<Bitmap> resizedBitmap;
public ButtonTable(Context context, Table table, int width, int height) {
super(context);
this.table=table;
this.width= width;
this.height= height;
setWidth(width);
setHeight(height);
setBitmapImage();
setBackgroundColor(INVISIBLE);
}
[...]
public void setBitmapImage(){
int resurce= R.drawable.rectangle6;
if(table.getTableType().equals("Circle6")){
resurce=R.drawable.circle6;
adjustmentForm= true;
}
if(table.getTableType().equals("Circle4")){
resurce= R.drawable.circle4;
adjustmentForm= true;
}
[...]
bmp = BitmapFactory.decodeResource(getResources(), resurce);
if(adjustmentForm){
bmp = Bitmap.createScaledBitmap(bmp, height, height, true);
}else{
bmp = Bitmap.createScaledBitmap(bmp,width, height, true);
}
resizedBitmap = new WeakReference<Bitmap>(bmp);
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
if(adjustmentForm){
canvas.drawBitmap(resizedBitmap.get(), ((width-height)/2), 0, null);
}else{
canvas.drawBitmap(resizedBitmap.get(), 0, 0, null);
}
}
}
Si je me déplace à travers les onglets pendant un certain temps, au bout de 10/15 onglet change, l'application arrête en raison d'un OutOfMemoryError. Pourquoi?
votre application dépasse la mémoire allouée pour l'application en tas et vous sortir d'exception de la mémoire. vous devez réduire l'image.http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html – Raghunandan
bien, pas dans ce cas ... parce qu'il n'appelle pas recycle() sur les bitmaps ... d'autres choses est que l'utilisation de WeakReference n'a aucun sens ici puisqu'il utilise aussi "pas de référence faible" – Selvin
regardez ici: http://stackoverflow.com/questions/5697760/android-out-of-memory-exception-when -creating-bitmap –