i have the folowing custom adapter for a Gallery widget. everything works great except when i change screen orientation, the app crashes due to
12-03 12:45:48.897: ERROR/AndroidRuntime(3594): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
i know that the maximum VM budget is 16 mb. 开发者_运维问答but how did i pass it? is there a way to clear the memory that is not used or on orientation change? i know there is a recycle method for bitmaps but how do i use it in my case?
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Uri[] mUrls;
String[] mFiles=null;
public ImageAdapter(Context c) {
mContext = c;
readImagesFromSd();
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public void readImagesFromSd(){
File images = new File(Environment.getExternalStorageDirectory().toString()+CameraView.FOLDER+"/");
images.mkdirs();
File[] imagelist = images.listFiles();
//Toast.makeText(mContext, String.valueOf(imagelist.length), Toast.LENGTH_LONG).show();
/*new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")));
}
});*/
mFiles = new String[imagelist.length];
for (int i = 0; i < imagelist.length; i++) {
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for (int i = 0; i < mFiles.length; i++) {
mUrls[i] = Uri.parse(mFiles[i]);
}
}
public int getCount() {
// return mImageIds.length;
return mUrls.length;
}
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);
// i.setImageResource(mImageIds[position]);
i.setImageURI(mUrls[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
精彩评论