2010-03-05 145 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中获得一些奇怪的路径,在这种情况下不会显示任何内容。