0

更新:2012年10月12日內存溢出的Android

與圖片製作後:

WeakHashMap<Bitmap, WeakReference<Bitmap>> objReferenciaDebilBitmaps; 
Bitmap imagen = datossss.getImagen(); 
objreCReferencias.RegristrarReferenciaDebilBitmap(imagen, objReferenciaDebilBitmaps); 

我的課Creferencias

public class CReferencias { 


public CReferencias(){ 

} 

public void RegristrarReferenciaDebilBitmap(Bitmap img, WeakHashMap<Bitmap, WeakReference<Bitmap>> objReferenciaDebilBitmaps){ 
    WeakReference<Bitmap> objrererencia1 = new WeakReference<Bitmap>(img); 
    objReferenciaDebilBitmaps.put(img, objrererencia1); 

} 

使用該我做的我創建的每一個時間之後一個圖像,我不知道這是否是正確的方式使用弱點引用位圖,

但仍然給我sigiente

的問題,這是我的logcat

12-10 13:54:53.523: E/dalvikvm-heap(1211): Out of memory on a 2304016-byte allocation. 
12-10 13:54:53.528: E/AndroidRuntime(1211): FATAL EXCEPTION: main 
12-10 13:54:53.528: E/AndroidRuntime(1211): java.lang.OutOfMemoryError 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.graphics.Bitmap.nativeCreate(Native Method) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.graphics.Bitmap.createBitmap(Bitmap.java:605) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at dep.Agenda.CustomAdapterDetalleEventos.getView(CustomAdapterDetalleEventos.java:558) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.AbsListView.obtainView(AbsListView.java:2201) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.ListView.makeAndAddView(ListView.java:1774) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.ListView.fillUp(ListView.java:705) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.ListView.fillGap(ListView.java:645) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5263) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.widget.AbsListView$TwFlingRunnable.run(AbsListView.java:4156) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.os.Handler.handleCallback(Handler.java:605) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.os.Looper.loop(Looper.java:137) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at android.app.ActivityThread.main(ActivityThread.java:4507) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
12-10 13:54:53.528: E/AndroidRuntime(1211):  at dalvik.system.NativeStart.main(Native Method) 

這行我的適配器

public View getView(final int position, View convertView, ViewGroup parent){ 
     View vistaFila =null; 

    final ElementosLinea objelementos;//=null; 

     //final ElementosLinea obj; 
     //final View auxVistaFila=null; 


    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 
      // LayoutInflater inflator = context.getLayoutInflater(); 


      //LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      vistaFila = vi.inflate(R.layout.ll_lineas_lista_detalle_evento_nueva, null); 

      //AsignarElementos(vistaFila); 

      objelementos = new ElementosLinea(); 
      AsignarElementos(vistaFila,objelementos); 




     vistaFila.setTag(objelementos); 
     objelementos.posicion= position; 

     }else{ 
      //vistaFila = vi.inflate(R.layout.ll_lineas_lista_detalles_evento, null); 
      vistaFila=convertView; 
      objelementos=(ElementosLinea)convertView.getTag(); 
      objelementos.posicion= position; 
     } 

     DetalleEvento datossss = arrayDatos.get(position); 

     if (datossss.getGusta()==0){ 

      objelementos.btnMegusta.setBackgroundResource(R.drawable.megustagris); 
      objelementos.txtMegusta.setText("Me gusta"); 
     }else{ 
      objelementos.btnMegusta.setBackgroundResource(R.drawable.megustaazul); 
      objelementos.txtMegusta.setText("No me gusta"); 
      } 


     //llenamos la barra de progreso 
    if (datossss.getTipo()==1){//SI ES UN VIDEO 

     objelementos.txtTitulo.setText(datossss.getTituloPost()); 
     objelementos.txtAutor.setText(datossss.getAutor()); 
     objelementos.txtPost.setVisibility(View.INVISIBLE); 

     if (datossss.getImagen()!=null){ 
      Bitmap imagen = datossss.getImagen(); 
      objreCReferencias.RegristrarReferenciaDebilBitmap(imagen, objReferenciaDebilBitmaps); 
      int width = imagen.getWidth(); 
       int height = imagen.getHeight(); 
       int newWidth = activiadPricipal.getWindowManager().getDefaultDisplay().getWidth(); 
       int newHeight = newWidth-80; 

       float scalarw=((float) newWidth) /width; 
       float scalarh=((float) newHeight) /height; 

      Matrix ma = new Matrix(); 
      ma.postScale(scalarw, scalarh); 

      Bitmap res = Bitmap.createBitmap(imagen, 0, 0, 
        width, height, ma, true); 
     objreCReferencias.RegristrarReferenciaDebilBitmap(res, objReferenciaDebilBitmaps); 
     objelementos.imgfotoVideo.setImageBitmap(res); 
     //imagen.recycle(); 
     //res.recycle(); 
     objelementos.btnComentarios.setEnabled(true); 
     objelementos.pgrprogreso.setVisibility(View.INVISIBLE); 

     }else{ 


       int newWidth = activiadPricipal.getWindowManager().getDefaultDisplay().getWidth(); 
       int newHeight = newWidth-80; 

       objelementos.imgfotoVideo.setImageBitmap(null); 
       objelementos.imgfotoVideo.setMinimumHeight(newHeight); 
       objelementos.imgfotoVideo.setMinimumWidth(newWidth); 
       objelementos.pgrprogreso.setVisibility(View.VISIBLE); 
     objelementos.btnComentarios.setEnabled(false); 
     } 
     objelementos.imgfotoVideo.setVisibility(View.VISIBLE); 
     objelementos.btnVerVideo.setVisibility(View.VISIBLE); 
     objelementos.txtFecha.setText(datossss.getFecha()); 
     objelementos.btnVerfoto.setVisibility(View.INVISIBLE); 




    }else{ if (datossss.getTipo()==2){ //SI ES UN POST 

     objelementos.txtTitulo.setText(datossss.getTituloPost()); 
     objelementos.txtAutor.setText(datossss.getAutor()); 
     objelementos.txtPost.setText(datossss.getTexto()); 
     objelementos.txtPost.setVisibility(View.VISIBLE); 
     objelementos.imgfotoVideo.setVisibility(View.INVISIBLE); 
     objelementos.imgfotoVideo.setImageBitmap(null); 
     objelementos.btnVerVideo.setVisibility(View.INVISIBLE); 
     objelementos.txtFecha.setText(datossss.getFecha()); 
     objelementos.btnVerfoto.setVisibility(View.INVISIBLE); 
     objelementos.btnComentarios.setEnabled(true); 

     objelementos.imgfotoVideo.setMinimumHeight(0); 
      objelementos.imgfotoVideo.setMinimumWidth(0); 

      objelementos.pgrprogreso.setVisibility(View.INVISIBLE); 
     //rtlCuerpo.set 






    }else{//SI ES UNA FOTO 

     objelementos.txtTitulo.setText(datossss.getTituloPost()); 
     objelementos.txtAutor.setText(datossss.getAutor()); 
     objelementos.imgfotoVideo.setVisibility(View.VISIBLE); 
     objelementos.btnVerfoto.setVisibility(View.VISIBLE); 


     if (datossss.getImagen()!=null){ 
     Bitmap imagen = datossss.getImagen(); 
     objreCReferencias.RegristrarReferenciaDebilBitmap(imagen, objReferenciaDebilBitmaps); 
     int width = imagen.getWidth(); 
      int height = imagen.getHeight(); 
      int newWidth = activiadPricipal.getWindowManager().getDefaultDisplay().getWidth(); 
      int newHeight = newWidth-80; 

      float scalarw=((float) newWidth) /width; 
      float scalarh=((float) newHeight) /height; 

     Matrix ma = new Matrix(); 
     ma.postScale(scalarw, scalarh); 

     Bitmap res = Bitmap.createBitmap(imagen, 0, 0, 
       width, height, ma, true); 

     objreCReferencias.RegristrarReferenciaDebilBitmap(res, objReferenciaDebilBitmaps);  
     objelementos.imgfotoVideo.setImageBitmap(res); 
     //imagen.recycle(); 
     //res.recycle(); 
     objelementos.btnComentarios.setEnabled(true); 
     objelementos.pgrprogreso.setVisibility(View.INVISIBLE); 
     }else{ 
       int newWidth = activiadPricipal.getWindowManager().getDefaultDisplay().getWidth(); 
       int newHeight = newWidth-80; 

       objelementos.imgfotoVideo.setImageBitmap(null); 
       objelementos.imgfotoVideo.setMinimumHeight(newHeight); 
       objelementos.imgfotoVideo.setMinimumWidth(newWidth); 


      objelementos.btnComentarios.setEnabled(false); 
      objelementos.pgrprogreso.setVisibility(View.VISIBLE); 
     } 
     objelementos.txtPost.setVisibility(View.INVISIBLE); 
     objelementos.btnVerVideo.setVisibility(View.INVISIBLE); 
     objelementos.txtFecha.setText(datossss.getFecha()); 



    } 



    } 

    if (datossss.getImagenAvatar()!=null){ 
     objelementos.imgAvatar.setImageBitmap(datossss.getImagenAvatar()); 
    }else{ 
     objelementos.imgAvatar.setImageResource(R.drawable.avatarpredeterminado); 
    } 


    objelementos.txtTitulo.setText(datossss.getTituloPost()); 



    return vistaFila; 



     } 

的objelementos相當於viewholder

這就是我什麼做錯了?我可能會說,對於所有圖像使用弱引用的正確方法是,當我不需要時將其刪除?

回答

1

如果你在你的應用程序中使用bitmaps,不要忘記調用bmp.recycle()GC Android中不能再利用的位圖(用於<的Android 4.0版本,從4.0開始,位圖是DVM的一部分)。同樣的問題也見我的答案。我已經解決了很多答案,它一定會解決你的問題。 bitmap size exceeds Vm budget error android

+0

謝謝,很好的幫助 –

+0

歡迎您:)如果您覺得這個答案有用,請不要忘記對它進行投票。 ;) – Shrikant

+0

請看我編輯的答案。 – Shrikant

0

看起來你的適配器效率不高。你是否像api演示中一樣實現了ViewHolder? 你可以編輯你的帖子並粘貼你的適配器,所以我們可以看到這是否是問題所在。

0

我認爲問題是加載圖像,如果您加載的圖像沒有回收,那麼它將始終顯示OutOfMemoryError。所以你呈現圖像之前,請嘗試使用下面的代碼來縮小圖像:

/* 
    * Function used to reduce the size of the image to avoid memory out of 
    * bound exception. 
    */ 
    public static Bitmap decodeFile(String f) { 
     try { 
      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f), null, o); 
      // The new size we want to scale to 
      final int REQUIRED_SIZE = 150; 
      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_SIZE 
        && o.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale *= 2; 

      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     } catch (FileNotFoundException e) { 

     } 
     return null; 
    } 

請記住,通過按比例縮小的圖像質量將略有減少。