2010-03-05 142 views
0

我有一個市場上的應用程序,顯示用戶的SDcard在畫廊中的所有照片。最初我遇到了內存不足的問題,但通過增加BitmapFactory選項中的inSampleSize來緩解這些問題。但是,我仍然有間歇性的圖像報告不顯示,在某些情況下根本沒有。 Droid Eris用戶似乎有更大的比例存在問題。下面是加載所有照片,並伴隨ImageAdapter方法:Android:顯示來自SDcard的圖像

private void loadAllPhotos() { 
    setContentView(R.layout.add_pictures);  
    String[] projection = {MediaStore.Images.Thumbnails._ID}; 
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
      projection, 
      null,  
      null, 
      MediaStore.Images.Thumbnails.IMAGE_ID); 
    column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 
    int size = cursor.getCount(); 
    if (size == 0) { 
     Toast.makeText(thisContext, "Can't find pics on SDcard.", Toast.LENGTH_SHORT).show(); 
    } 
    g = (Gallery) findViewById(R.id.gallery); 
    g.setAdapter(new ImageAdapter(this)); 
} 

的ImageAdapter:

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    public ImageAdapter(Context c) { 
     mContext = c; 
     TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
     mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 
     a.recycle(); 
    } 
    public int getCount() { 
    int cursorCount = mCursor.getCount(); 
     return cursorCount; 
    } 
    public Object getItem(int position) { 
     return position; 
    } 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(mContext); 
     System.gc(); 
     if (convertView == null) {           
     try { 
      String [] proj={MediaStore.Images.Media.DATA}; 
      mCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,proj, null, null, null); 
      column_index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      mCursor.moveToPosition(position); 
      filename = mCursor.getString(column_index); 
      if (filename.endsWith(".jpg") || filename.endsWith(".png")) { 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 15; 
       Bitmap bm = BitmapFactory.decodeFile(filename, options); 
       i.setImageBitmap(bm); 
       i.setScaleType(ImageView.ScaleType.FIT_XY); 
       i.setLayoutParams(new Gallery.LayoutParams(150, 150)); 
       i.setBackgroundResource(mGalleryItemBackground); 
       System.gc(); 
      } 
     } catch (Exception e) { 
     } 
     } 
      return i; 
    } 
    } 

回答

2

如果您還沒有這樣做的話,集成亂舞或DroidDrop或東西收集例外,因爲他們可能會對您的問題提供一些啓示。

另外:

  1. 請回收您的Views!您每撥打getView()電話都會創建一個新的ImageView,這對您的GC狀況沒有幫助。
  2. 您每撥打getView()電話都會撥打相同的​​,這很可怕。請這樣做一次。
  3. getView()中初始化之前,您似乎正在訪問mCursorgetCount()
  4. column_index在撥打​​的方式中應始終爲0,因此您不需要使用getColumnIndexOrThrow()。即使你想使用它,只需要做一次,當你進行單一的​​調用。

是否有任何這些將清除您的Droid厄里斯問題,但我不能說。

+0

非常感謝您的反饋。我已經使用自定義錯誤報告,允許用戶在發生異常時向我發送堆棧跟蹤信息。 我會做出你所建議的改變,看看事情進展如何。 – polyclef 2010-03-05 22:43:28

0

我試過上面的代碼,只有在SD上只有.png和.jpg時,它才能正常工作。否則,它可以從mCursor中獲得一些奇怪的路徑,在這種情況下不會顯示任何內容。