包含来自 SD 卡的图像的图库的应用程序在方向更改时崩溃

发布于 2024-10-05 19:56:35 字数 2089 浏览 1 评论 0原文

我有以下用于图库小部件的自定义适配器。一切都很好,除了当我改变屏幕方向时,应用程序崩溃,因为

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文