机器人 - 非内存问题 [英] android - Out of memory problem

查看:128
本文介绍了机器人 - 非内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我下载来自Web的图片。对于这一点,我第一次从网上下载图像并将这些图像存储在SD卡。下一次,我检查这些图像是在SD卡或没有。如果是,从SD卡读取,否则我是从网上下载的。这些图片显示类似的列表。我是多次(指/下不断上移)滚动列表,然后我的应用程序崩溃,我得到了内存不足的异常。如何处理它。任何人可以帮助我。

堆栈跟踪:

  09-12 13:40:42.640:ERROR / AndroidRuntime(3426):java.lang.OutOfMemoryError:位图大小超过VM预算(堆大小= 7879KB,分配= 3362KB,位图大小= 11402KB)
09-12 13:40:42.640:ERROR / AndroidRuntime(3426):在android.graphics.BitmapFactory.nativeDe codeFILE(本机方法)
09-12 13:40:42.640:ERROR / AndroidRuntime(3426):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:355)
09-12 13:40:42.640:ERROR / AndroidRuntime(3426):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:433)
09-12 13:40:42.640:ERROR / AndroidRuntime(3426):在com.ibkr.elgifto.GiftCategories $ itemlistadapter $ 4.getDrawableFromUrl(GiftCategories.java:830)
09-12 13:40:42.640:ERROR / AndroidRuntime(3426):在com.ibkr.elgifto.GiftCategories $ itemlistadapter $ 4.run(GiftCategories.java:739)
09-12 13:53:32.450:信息/ WSP(332):[接收器]下一个自动同步告警事件是180分钟后,在时间:周一09月12日16时53分32秒GMT + 05:30 2011
 

code

 私人绘​​制对象getDrawableFromUrl(字符串IMAGEURL){

    // TODO自动生成方法存根
    字符串文件名= IMAGEURL;
    文件名= filename.replace(/,+);
    文件名= filename.replace(:,+);
    文件名= filename.replace(〜,S);
    文件elgiftoimagesref =新的文件(/ SD卡/ Elgiftoimages /);

    elgiftoimagesref.mkdir();

    最终文件的文件=新的文件(elgiftoimagesref,文件名);
    BitmapDrawable SDdrawable = NULL;

    布尔存在= file.exists();
    如果(!存在){
        尝试 {
            URL myFileUrl =新的URL(IMAGEURL);
            HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl
                    .openConnection();
            conn.setDoInput(真正的);
            conn.connect();
            InputStream的是= conn.getInputStream();
            最后的位图的结果= BitmapFactory.de codeStream(是);
            is.close();
            新的Thread(){
                公共无效的run(){
                    ByteArrayOutputStream字节=新ByteArrayOutputStream();
                    如果(结果!= NULL){
                        result.com preSS(Bitmap.Com pressFormat.JPEG,40,
                                字节);
                    }
                    尝试 {

                        FileOutputStream中FO;
                        FO =新的FileOutputStream(文件);
                        fo.write(bytes.toByteArray());

                        fo.flush();
                        fo.close();
                        // result.recycle();
                    }赶上(IOException异常E){
                        e.printStackTrace();
                    }
                }
            }。开始();
            BitmapDrawable returnResult =新BitmapDrawable(结果);
            返回returnResult;
        }赶上(FileNotFoundException异常E){
            e.printStackTrace();
        }赶上(MalformedURLException异常E){
            e.printStackTrace();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        返回null;
    } 其他 {

        //这里我得到了内存不足的错误。
        SDdrawable =新BitmapDrawable(BitmapFactory.de codeFILE(文件
                的ToString()));
        返回SDdrawable;
    }
}
 

解决方案

我觉得你不使用的 ViewHolder模型作为列表视图。在这种情况下,你应该在得到这样的错误,因为所有的列表项将在可用内存无论是可见还是not.By使用ViewHolder内存只分配到那些是可见的,是目前屏幕上的项目。 ViewHolder patternincreses 150%的性能 有关ViewHolder视频教程退房这个。如果你已经实现则是内存的情况下,泄漏你可以参考

In my app, i am downloading images from the web. For this, first time i am downloading images from the web and these images are stored in the sdcard. Next time, i am checking these images are in sdcard or not. if yes, fetching from the sdcard otherwise i am downloading from the web. These images are displayed like list. i am repeatedly(means move up/down continuously) scrolling the list then my app crashed and i am getting the out of memory exception. How to handle it. can anybody help me.

Stacktrace:

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): java.lang.OutOfMemoryError: bitmap size exceeds VM  budget(Heap Size=7879KB, Allocated=3362KB, Bitmap Size=11402KB)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:355)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:433)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  com.ibkr.elgifto.GiftCategories$itemlistadapter$4.getDrawableFromUrl(GiftCategories.java:830)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  com.ibkr.elgifto.GiftCategories$itemlistadapter$4.run(GiftCategories.java:739)
09-12 13:53:32.450: INFO/WSP(332): [Receiver] next auto-sync alarm event is 180 mins later, at time: Mon Sep 12 16:53:32 GMT+05:30 2011

code

private Drawable getDrawableFromUrl(String imageUrl) {

    // TODO Auto-generated method stub
    String filename = imageUrl;
    filename = filename.replace("/", "+");
    filename = filename.replace(":", "+");
    filename = filename.replace("~", "s");
    File elgiftoimagesref = new File("/sdcard/Elgiftoimages/");

    elgiftoimagesref.mkdir();

    final File file = new File(elgiftoimagesref, filename);
    BitmapDrawable SDdrawable = null;

    boolean exists = file.exists();
    if (!exists) {
        try {
            URL myFileUrl = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            final Bitmap result = BitmapFactory.decodeStream(is);
            is.close();
            new Thread() {
                public void run() {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    if (result != null) {
                        result.compress(Bitmap.CompressFormat.JPEG, 40,
                                bytes);
                    }
                    try {

                        FileOutputStream fo;
                        fo = new FileOutputStream(file);
                        fo.write(bytes.toByteArray());

                        fo.flush();
                        fo.close();
                        // result.recycle();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            BitmapDrawable returnResult = new BitmapDrawable(result);
            return returnResult;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } else {

        // Here i am getting the out of memory error.
        SDdrawable = new BitmapDrawable(BitmapFactory.decodeFile(file
                .toString()));
        return SDdrawable;
    }
}       

解决方案

I think you are not using the ViewHolder model for listview. In such case you are supposed the get such error as all the list items will be available in the memory whether it is visible or not.By using ViewHolder memory is assigned to only those items which are visible and are currently on the screen. ViewHolder patternincreses the performance by 150% For video tutorial about ViewHolder check out this .If you are already implemented then it is the case of memory leak you can refer this

这篇关于机器人 - 非内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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