内存不足的错误,由于大量的图像缩略图显示 [英] Out of memory error due to large number of image thumbnails to display

查看:1026
本文介绍了内存不足的错误,由于大量的图像缩略图显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下codeS被用来检索所有照片上显示的GridView的目的。但是,如果我有1000张照片,这将导致内存不足的错误。是否有任何人谁可以帮助做到这一点?

P.S。如果任何人都可以,有U能够显示编辑codeS为上述利用延迟加载和缓存的?我是pretty丢失。所述的initialise方法用于基本上设置将由gridview的要使用的ImageAdapter

谢谢!

 公共无效的initialize(){
        images.clear();
        最终的String [] =列{} MediaStore.Images.Thumbnails._ID;
        最后弦乐ORDERBY = MediaStore.Images.Media._ID;
        @燮pressWarnings(德precation)
        光标imagecursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,列,
                 NULL,NULL,排序依据);        如果(imagecursor!= NULL){
            INT image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);
            诠释计数= imagecursor.getCount();
            的for(int i = 0; I<计数;我++){
                imagecursor.moveToPosition(ⅰ);
                INT ID = imagecursor.getInt(image_column_index);
                的ImageItem的ImageItem =新的ImageItem();
                imageItem.id = ID;
                lastId = ID;
                imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                        getApplicationContext()。getContentResolver(),身份证,
                        MediaStore.Images.Thumbnails.MICRO_KIND,NULL);
                images.add(的ImageItem);
            }
            //imagecursor.close();
        }
        notifyDataSetChanged();
    }


解决方案

您正在运行内存,因为你加载很多,这样会占用大量内存位图。简单的(正确)的答案是:不要一次加载它们。相反,你应该装入适配器每个图像 必要

The following codes were used to retrieve all photos for the purpose of displaying on gridview. However, if i have 1000 photos, it would result in out of memory error. Is there anyone who could help with this?

P.S. if anyone could, are u able to show the edited codes for the above to make use of lazy loading and caching? I'm pretty lost. The initialise method is used basically to set the ImageAdapter that will be used by gridview

Thanks!!

    public void initialize() {
        images.clear();
        final String[] columns = { MediaStore.Images.Thumbnails._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        @SuppressWarnings("deprecation")
        Cursor imagecursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                 null, null, orderBy);

        if(imagecursor != null){
            int image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);
            int count = imagecursor.getCount();
            for (int i = 0; i < count; i++) {
                imagecursor.moveToPosition(i);
                int id = imagecursor.getInt(image_column_index);
                ImageItem imageItem = new ImageItem();
                imageItem.id = id;
                lastId = id;
                imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                        getApplicationContext().getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, null);
                images.add(imageItem);
            }
            //imagecursor.close();
        }
        notifyDataSetChanged();
    }

解决方案

You're running out of memory because you're loading lots of bitmaps that take up lots of memory. The simple (and correct) answer is: don't load them all at once. Instead you should load each image in the adapter as necessary.

这篇关于内存不足的错误,由于大量的图像缩略图显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆