Android:在Gallery中使用位图的outOfMemoryError

时间:2011-10-11 11:01:43

标签: java android bitmap out-of-memory

我有一个应用程序从服务器下载图像,将它们存储在SD卡上,然后以幻灯片形式显示给用户。该应用程序有许多不同的幻灯片,用户可以查看。我的问题是,在我查看几个画廊后,我发现内存不足错误。我做了很多谷歌搜索,并阅读了Romain Guy的Avoiding Memory Leaks文章大约20次。我试图复制他的unbindDrawables()方法,但是无法在Gallery对象上调用removeAllViews()。我也尝试在所有位图上调用recycle()但是当我在适配器中的位图上执行时,应用程序会在打开图库时立即抛出错误。 我也尝试重新编码幻灯片,以便在适配器外部创建所有位图,然后将它们作为数组传递 - 这允许我遍历我的位图数组并在幻灯片中的onDestroy方法中对每个位图调用recycle()活动 - 但这实际上似乎使泄漏变得更糟。

以下是我的幻灯片活动的代码:

public class Slideshow extends Activity {
    static String galleryId;
    public static final int MSG_DOWNLOADED = 0;
    static  Handler handler;
    static Gallery g;
    static ArrayList<String> filePaths;
    static String subFolder;
    static ArrayList<String>  imageToGet;
    static LinearLayout pb;
    static boolean firstTime = true;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.slideshow);
            Context context = getApplicationContext();


            Bundle extras = getIntent().getExtras();

            if(extras != null){
            galleryId = extras.getString("galleryId");
            }
            pb = (LinearLayout) findViewById(R.id.progress); 
             handler = new Handler(){
                    @Override
                    public void handleMessage(Message msg){
                        switch (msg.what){
                        case MSG_DOWNLOADED:
                            g.setAdapter(new SlideshowAdapter(getApplicationContext(), R.id.gallery, filePaths));
                            pb.setVisibility(8);
                            if(firstTime){
                            Thread images = new Thread(){
                                public void run(){
                                    ImageGallery gallery = XMLParser.getGalleryById(galleryId, false, getApplicationContext());
                                    ArrayList<String> imageURLs = gallery.getImageURLs();
                                    String subFolder = "gallery"+galleryId+"/";
                                    getImages(imageURLs, subFolder, false);
                                }
                            };
                            images.start();
                            firstTime = false;
                            }
                            break;
                        }
                    }
                };
            ImageGallery gallery = XMLParser.getGalleryById(galleryId, false, context);
            filePaths = gallery.getFilePaths();
            ArrayList<String> imageURLs = gallery.getImageURLs();
            subFolder = "gallery"+galleryId+"/";
            g = (Gallery) findViewById(R.id.gallery);
            if(filePaths.size() > 0){
            String filePath = filePaths.get(0);
            String url = imageURLs.get(0);
            imageToGet = new ArrayList<String>();
            imageToGet.add(url);

            if(filePaths.size() == 1 ){
                File file = new File(filePath);
                if(!file.exists()){
                    Thread getFirstImage = new Thread(){
                        public void  run(){
                            Log.d("ClubSlideshow getting only image", ""+imageToGet.get(0));
                            getImages(imageToGet, subFolder, false);
                            handler.sendEmptyMessage(MSG_DOWNLOADED);
                        }
                    };
                    getFirstImage.start();
                }

            }else if(filePaths.size()>1){
                String filePath2 = filePaths.get(1);
                String url2 = imageURLs.get(1);
                File file = new File(filePath);
                File file2 = new File(filePath2);
                imageToGet.add(url2);
                if(!file.exists()||!file2.exists()){
                    Thread getFirstImage = new Thread(){
                        public void  run(){
                            getImages(imageToGet, subFolder, false);
                            handler.sendEmptyMessage(MSG_DOWNLOADED);
                        }
                    };
                    getFirstImage.start();
                }

            }
            }


    }

    public void getImages(ArrayList<String> imageURLs, String subFolder, boolean force){
        DataCache.downloadFromUrlArray(imageURLs, subFolder, force, getApplicationContext());
    }

    public class ImageThread implements Runnable{
        public ImageThread(ArrayList<String> imageURLs, String subFolder, Boolean force){
            getImages(imageURLs, subFolder, force);
        }

        public void run(){

        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        g.setAdapter(null);
    }

}

这是我的SlideshowAdapter类的代码:

public class SlideshowAdapter extends ArrayAdapter<String> {
    int mGalleryItemBackground;
    private Context mContext;
    private ArrayList<String> images = new ArrayList<String>();
    Bitmap bMap;
    String filePath;
    File file;
    ImageView i;

    public SlideshowAdapter(Context c, int resourceId, ArrayList<String> objects){
        super(c, resourceId, objects);
        this.mContext = c;
        this.images = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView !=null){
            i = (ImageView) convertView;
        }else{
            i = new ImageView(mContext);
        }

        filePath = mContext.getFilesDir()+"/"+images.get(position);

        file = new File(filePath);
        if(file.exists()){
            bMap = BitmapFactory.decodeFile(filePath);
            i.setImageBitmap(bMap);
        }

        i.setScaleType(ImageView.ScaleType.FIT_CENTER);

        return i;
    }

}

有人能看到可能导致记忆问题的原因吗?

3 个答案:

答案 0 :(得分:5)

实际上有几个问题:

  • 图库视图缓存已损坏,因此每次调用getView方法时都会如此 convertView为null请参阅this bug
  • 在适配器中,在每个getView方法调用上分配新的位图 所以然后滚动发生相同的位图一次又一次地解码 所以从我的角度来看,你应该尝试在你的适配器中实现你自己的Bitmap缓存,这样内存不会被相同的位图对象推翻

答案 1 :(得分:1)

您可以尝试使用 scaledBitmap 来减少内存使用量

if(file.exists()){ 
            bMap = BitmapFactory.decodeFile(filePath); 
            i.setImageBitmap(bMap); 
        } 

if(file.exists()){ 
            bMap = BitmapFactory.decodeFile(filePath); 
            bMap = Bitmap.createScaledBitmap(bMap, 100, 100, true);
            i.setImageBitmap(bMap); 
        } 

答案 2 :(得分:0)

尝试将图像存储在SD卡或设备内存中以避免此问题。

相关问题