包含来自 SD 卡的图像的图库的应用程序在方向更改时崩溃
我有以下用于图库小部件的自定义适配器。一切都很好,除了当我改变屏幕方向时,应用程序崩溃,因为
12-03 12:45:48.897: ERROR/AndroidRuntime(3594): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
我知道最大虚拟机预算是 16 mb。但我是怎么通过的呢?有没有办法清除未使用或方向改变时的内存?我知道有一种位图回收方法,但在我的情况下如何使用它?
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论