Android Java Gallery 内存不足错误

发布于 2024-10-10 12:24:32 字数 1205 浏览 6 评论 0原文

所以我将图像从 SD 卡加载到 Android Gallery 中。我点击菜单,然后选择它,它通过下面的代码插入到图库中。问题是在大约 5 或 6 个图像之后,我收到内存不足错误 01-04 18:10:35.246: ERROR/AndroidRuntime(10220): java.lang.OutOfMemoryError: 位图大小超出 VM 预算 关于我能做什么的任何想法解决这个问题吗?

public class ImageAdapter extends BaseAdapter{  

    int mGalleryItemBackground;  
    public ImageAdapter(Context c)  {     
        mContext = c;     
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        mGalleryItemBackground = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }  
    public int getCount(){  
        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.setImageURI(mUrls[position]); 
        i.setScaleType(ImageView.ScaleType.FIT_XY);  
        i.setLayoutParams(new Gallery.LayoutParams(120, 120));  
        return i;  
    }     
    private Context mContext;  
    }     

So I'm loading images into Android Gallery from SD card. I hit menu and then select and it's inserted into Gallery via code below. Problem is after about 5 or 6 images I get a out of memory error 01-04 18:10:35.246: ERROR/AndroidRuntime(10220): java.lang.OutOfMemoryError: bitmap size exceeds VM budget Any ideas on what I can do to resolve this?

public class ImageAdapter extends BaseAdapter{  

    int mGalleryItemBackground;  
    public ImageAdapter(Context c)  {     
        mContext = c;     
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        mGalleryItemBackground = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }  
    public int getCount(){  
        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.setImageURI(mUrls[position]); 
        i.setScaleType(ImageView.ScaleType.FIT_XY);  
        i.setLayoutParams(new Gallery.LayoutParams(120, 120));  
        return i;  
    }     
    private Context mContext;  
    }     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-10-17 12:24:32

您要插入图库中的图像的宽度和高度(以像素为单位)是多少?如果它们非常大,可能是 500x500 或更大,那么我建议在将它们添加到图库之前将它们调整为更小且更接近您预期大小的 120x120。

您可以使用以下方法缩小它们 Bitmap.createScaledBitmap

编辑:请注意,这与让 ImageView 进行缩放不同。 ImageView 将在显示时缩放位图,但将原始位图保留在堆内存中。如果您使用createScaledBitmap并丢弃原始位图,那么您将节省大量堆。

What is the width and height in pixels of the images you are inserting into the Gallery? If they are very large, perhaps 500x500 or larger then I'd suggest resizing them to something smaller and closer to your intended size of 120x120 before adding them to the Gallery.

You can scale them down using the method Bitmap.createScaledBitmap.

EDIT: Note that this is different from having the ImageView do the scaling. The ImageView will scale the bitmap as it is displayed but keeps the original bitmap in heap memory. If you use createScaledBitmap and throw the original bitmap away then you'll be saving a lot of heap.

梦里人 2024-10-17 12:24:32

尽量避免使用

private Context mContext;

将 Context/Activity 存储为类的私有成员是非常糟糕的做法。建议仅通过参数使用上下文/活动。存储 Context 的典型结果是内存泄漏,我相信这演示了您的代码。您可以在此处阅读更多内容。

Try to avoid usage of

private Context mContext;

It's very bad practice to store Context/Activity as private members of classes. Context/Activity recommended to use only through parameters. Typical result of storing Context is a memory leaks, which I believe demonstrates your code. More you can read here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文