У меня есть приложение на рынке, которое отображает все фотографии с SD-карты пользователя в галерее.Изначально у меня были проблемы с нехваткой памяти, но я решил их, увеличив inSampleSize в параметрах BitmapFactory.Тем не менее, у меня до сих пор периодически появляются сообщения о том, что изображения не отображаются, а в некоторых случаях вообще нет.Кажется, есть большая доля пользователей Droid Eris, имеющих проблемы.Вот метод, который загружает все фотографии и сопровождающий ImageAdapter:
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;
}
}