2013-08-22 74 views
1

我通過許多資源推薦,但無法得到正確的答案,滾動Listview時內存不足異常?

我已經做了一個自定義適配器來查看圖像在列表視圖。這些圖像從存儲卡中檢索。一切運行良好,但是當我滾動列表視圖時,我得到一個OutOfMemory異常。我發佈了代碼,使用我從SD卡中檢索圖像。

public void getFromSdcard() { 

    File file = new File(
      android.os.Environment.getExternalStorageDirectory(), 
      "Tiles/.NoMedia"); 

    if (file.isDirectory()) { 
     listFile = file.listFiles(); 

     for (int i = 0; i < listFile.length; i++) { 

      f.add(listFile[i].getAbsolutePath()); 

     } 
    } 
} 

這裏f是字符串數組列表,我將它傳遞給下一個自定義適配器是我的自定義接口的代碼。

public class NewImageAdapter extends ArrayAdapter<Image> { 

private ArrayList<Image> objects; 
String packageName; 
Activity act; 

public NewImageAdapter(Activity context, int image_layout, 
     ArrayList<Image> objects) { 
    super(context, image_layout, objects); 
    this.act = context; 
    this.objects = objects; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View v = convertView; 

    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.list_item, null); 
    } 

    Image i = objects.get(position); 

    if (i != null) { 
     ImageView iv = (ImageView) v.findViewById(R.id.imagemenu123); 
     // TextView tv = (TextView) v.findViewById(R.id.commandText); 
     if (iv != null) { 

      Bitmap myBitmap = BitmapFactory.decodeFile(i.getImagePath()); 

      iv.setImageBitmap(myBitmap); 
      // iv.setImageBitmap(i.getImageBitmap()); 
      // tv.setText("Tiles Images"); 
     } 

    } 
    return v; 
} 

}

任何解決我的問題:

回答

5

使用這個概念,這將幫助你,在那之後設置圖像視圖

public static Bitmap convertBitmap(String path) { 

     Bitmap bitmap=null; 
     BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 
     bfOptions.inDither=false;      //Disable Dithering mode 
     bfOptions.inPurgeable=true;     //Tell to gc that whether it needs free memory, the Bitmap can be cleared 
     bfOptions.inInputShareable=true;    //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 
     bfOptions.inTempStorage=new byte[32 * 1024]; 


     File file=new File(path); 
     FileInputStream fs=null; 
     try { 
      fs = new FileInputStream(file); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     try { 
      if(fs!=null) 
      { 
       bitmap=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions); 
      } 
      } catch (IOException e) { 

      e.printStackTrace(); 
     } finally{ 
      if(fs!=null) { 
       try { 
        fs.close(); 
       } catch (IOException e) { 

        e.printStackTrace(); 
       } 
      } 
     } 

     return bitmap; 
    } 

的imagebitmap如果你想從高度和寬度像60和60大的圖像小圖像和滾動列表視圖快然後使用這個概念

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
      int reqHeight) { 

     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap bmp = BitmapFactory.decodeFile(path, options); 
     return bmp; 
     } 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 

     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
      } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
      } 
     } 
     return inSampleSize; 
     } 

我希望它會幫助你很多。

您凸輪採取從開發者網站Here

+0

希望它的工作.... Thanx ... @ Sunil ... –

+0

U歡迎投票... –

0

出的存儲裝置,以及你需要加載圖像在ListView只有當他們在視圖,並考慮釋放他們時,沒有 - 動態加載它們。

您也可以嘗試在該適配器中合併ViewHolder類。

0

可能是你可以試試這個:

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) 
iv.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false)); 

希望它幫助!

+0

謝謝你,但我已經嘗試過這種解決方案,這不適合我... –

+0

我認爲你應該發佈logcat的問題,以更好的解決問題。 –

-1

幫助其實我覺得最好的辦法是找到錯誤振振有辭。對於這個「Out of Memroy」的主要共同點是,當你滾動gcc分配內存時,即使你從下往上滾動,它也會爲listview分配單獨的內存,換句話說,你的memeory會被浪費或多餘的使用。即使在位圖中,您可以通過縮放選項減少emeroy分配大小,但它是永久性解決方案的短期解決方案,我發現通過查找d卡中的圖像數量以及滾動比較poistion時爲每個listview創建視圖數組當poistion的大小等於view-1的大小時,你已經分配了end net unique memeroy,如果你再次向上或向下滾動,則不會在listview中分配memroy,因爲你已經到達視圖數組的末尾,沒有furthur需要分配memeroy,因此問題解決

+0

我不說wo,但我想明確說明我有解釋你背後真正的問題內存不足錯誤,並且使用位圖提供長期解決方案並不能解決問題 – Joker