如何避免内存不足异常 [英] How to avoid the out of memory exception

查看:140
本文介绍了如何避免内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我下载使用图像的URL网页中的图像。我有大量的图片,所以我实现分页技术,这和我15显示的图像进行垂直顺序中的每个页面。在这种情况下,我滚动向上/向下的页面,此时我的应用程序崩溃查看图像和我得到了内存不足的异常。请任何人可以帮助我。

logcat的:

  11月2日至7日:23:52.256:ERROR / ACRA(7236):萨尔瓦多Gifto致命错误:位图大小超过VM预算(堆大小= 7943KB,分配= 3485KB,位图大小= 12546KB)
11月2日至7日:23:52.256:ERROR / ACRA(7236):java.lang.OutOfMemoryError:位图大小超过VM预算(堆大小= 7943KB,分配= 3485KB,位图大小= 12546KB)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:690)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:490)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在com.ibkr.elgifto.GiftCategories $ itemlistadapter $ 3.getDrawableFromUrl(GiftCategories.java:837)
11月2日至7日:23:52.256:ERROR / ACRA(7236):在com.ibkr.elgifto.GiftCategories $ itemlistadapter $ 3.run(GiftCategories.java:724)

下面是我的code:

 公共无效DownLoadImageInAThreadHandler(最终CategoryData项目,最终ViewHolder持有人)
{
    最后的处理程序处理程序=新的处理程序()
    {
        @覆盖公共无效的handleMessage(消息消息)
        {
            holder.imgitem.setImageDrawable((绘制对象)message.obj);
            holder.imgitem.setVisibility(View.VISIBLE);
            holder.progress.setVisibility(View.GONE);
        }
     };     //线程获取的属性值
     线程t =新的Thread()
     {
        公共无效的run()
        {
            尝试
            {
                绘制= getDrawableFromUrl(Item.ImageUrl);                如果(绘!= NULL)
                {
                    //发送消息到处理程序
                    消息消息= handler.obtainMessage(1,绘制);
                    handler.sendMessage(消息);
                }
                其他
                {
                    INT idNoImage = R.drawable.giftsuggestionsnoimage;
                    。可绘制dwgNoImg = GiftCategories.this.getResources()getDrawable(idNoImage);                    //发送消息到处理程序
                    消息消息= handler.obtainMessage(1 dwgNoImg);
                    handler.sendMessage(消息);
                }            }
            赶上(例外EXP)
            {
                的System.out.println(异常在DownLoadImageInAThread:+ exp.getMessage());
            }
        }        私人绘制对象getDrawableFromUrl(字符串图片网址)抛出IOException异常
        {
            绘制的图像= NULL;            尝试
            {
                InputStream的时间=(java.io.InputStream中)新的java.net.URL(图片网址).getContent();
                如果(在!= NULL)
                {
                    图像= Drawable.createFromStream(在图像);
                    附寄();
                }
            }
            赶上(异常前)
            {
                ex.printStackTrace();
            }
            返回形象;
        }
    };
    t.start();
}


解决方案

在pre ICS手机分别对Dalvik的堆上创建位图和它们的内存清理被称为这是不是银布勒<一个终结HREF =HTTP://$c$c.google.com/p/android/issues/detail ID = 8488相对=nofollow> HTTP://$c$c.google.com/p/android /问题/细节?ID = 8488

使用位图对象,并确保调用recycle方法

In My application i am downloading the images from the web using url of the image. I have lot of images So i implemented the paging technique for this and i displayed 15 images for each page in vertical order. In this case i am scrolling up/down the page to view the images at this time my app is crashed and i got out of memory exception. please can anybody help me.

Logcat:

02-07 11:23:52.256: ERROR/ACRA(7236): El Gifto fatal error : bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)    
02-07 11:23:52.256: ERROR/ACRA(7236): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:690)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:490)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.getDrawableFromUrl(GiftCategories.java:837)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.run(GiftCategories.java:724)

Here is my code:

public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{   
    final Handler handler = new Handler() 
    {             
        @Override  public void handleMessage(Message message) 
        {                 
            holder.imgitem.setImageDrawable((Drawable) message.obj);   
            holder.imgitem.setVisibility(View.VISIBLE);
            holder.progress.setVisibility(View.GONE);
        }
     };

     //Thread for getting the attributes values
     Thread t = new Thread() 
     {
        public void run()
        {                       
            try
            {                                       
                drawable = getDrawableFromUrl(Item.ImageUrl);                                                       

                if(drawable != null)
                {                                                               
                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, drawable);                 
                    handler.sendMessage(message);                                                               
                }
                else
                {
                    int idNoImage = R.drawable.giftsuggestionsnoimage;
                    Drawable dwgNoImg = GiftCategories.this.getResources().getDrawable(idNoImage);

                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, dwgNoImg);                 
                    handler.sendMessage(message); 
                }                        

            }
            catch(Exception exp)
            {
                System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
            }
        }

        private Drawable getDrawableFromUrl(String imageUrl) throws IOException
        {                   
            Drawable image = null;

            try 
            {
                InputStream in = (java.io.InputStream) new java.net.URL(imageUrl).getContent();
                if (in != null)
                {
                    image = Drawable.createFromStream(in, "image");
                    in.close();
                }
            } 
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
            return image;
        }                                                               
    };
    t.start();                                              
}

解决方案

On pre ICS phones the bitmaps were created on the dalvik heap and their memory cleaning was called in the finalizer which isn't a silver buller http://code.google.com/p/android/issues/detail?id=8488

Use a bitmap object and make sure call the recycle method

这篇关于如何避免内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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