Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView OutOfMemoryError:位图大小超过VM预算_Android_Listview_Bitmap_Out Of Memory - Fatal编程技术网

Android ListView OutOfMemoryError:位图大小超过VM预算

Android ListView OutOfMemoryError:位图大小超过VM预算,android,listview,bitmap,out-of-memory,Android,Listview,Bitmap,Out Of Memory,好的,这是一个非常常见的问题,但我的问题有点不同,我找不到其他主题的解决方案,所以我在这里发布了一个新的。我有一个显示ListView的应用程序。每行ListView都有一个ImageView,可以使用ListAdapter从SD卡加载一个小位图图标(它很小,所以问题不在于大小)。现在,如果我慢慢滚动列表,它可以正常工作。但是如果我滚动得很快,当ListView足够长时,它不再显示图标,logcat中的消息是这样的: 126 600-byte external allocation too la

好的,这是一个非常常见的问题,但我的问题有点不同,我找不到其他主题的解决方案,所以我在这里发布了一个新的。我有一个显示ListView的应用程序。每行ListView都有一个ImageView,可以使用ListAdapter从SD卡加载一个小位图图标(它很小,所以问题不在于大小)。现在,如果我慢慢滚动列表,它可以正常工作。但是如果我滚动得很快,当ListView足够长时,它不再显示图标,logcat中的消息是这样的:

126 600-byte external allocation too large for this process.
VM不允许我们分配126600字节

然后,应用程序崩溃和logcat显示:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
我在两台不同的设备上进行了测试,其中只有一台出现了这个错误,另一台正常工作。 请注意,此错误仅在ListView快速滚动时发生。这是因为创建的新线程与垃圾收集的速度不匹配还是其他原因?
在这种情况下,有人能给我一些建议吗?

在使用位图时,当使用ListView时,您大部分都会失去记忆

所以你应该在这里表演

将负责您的图像加载

要使用SD卡,您需要替换ImageLoader类中的一个方法,如下所示

private Bitmap getBitmap(String url) 
    {
        // If is from SD Card
        try {
            File file = new File(url);
            if(file.exists())
            {
                return BitmapFactory.decodeFile(url);
            }
        } catch (Exception e) {

        }

        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

@帕雷什·马亚尼:你这是什么意思?我对android真的很陌生,所以在提问时请更具体一些,之前请做一些搜索,这里已经有很多问题了:检查屏幕右上方的搜索框。。它会回答你的!!当然,我已经搜索了其他主题,但它们没有帮助,这就是为什么我必须发布新主题的原因。我在我的帖子中说过。是否可以在ListAdapter的getView中发布代码?在这里,您应该重用传递的视图,并使用setTag保留位图的引用,并在设置为新的位图之前回收位图。我的方法与您的方法相同,但没有解决问题