热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从视图中的路径加载位图时内存泄漏-Memoryleakwhenloadingbitmapsfrompathinviewholder

Imtryingtofindoutforagesnowwhyigotthismemoryleakintheviewholderofmylistview.我试图

I'm trying to find out for ages now why i got this memory leak in the viewholder of my listview.

我试图找出多年来为什么我在listview的视图中出现了这个内存泄漏。

The strange part is that when setting the imageview (coverIv) with ((BitmapDrawable)_activity.Resources.GetDrawable(Resource.Drawable.splash)).Bitmap, there's no problem at all.

奇怪的是,当使用((BitmapDrawable)_activity.Resources.GetDrawable(Resource.Drawable.splash))。Bitmap设置imageview(coverIv)时,根本没有问题。

When i use await ImageLoader.DecodeSampledBitmapFromResourceAsync (localImageLocation, imgWidth, imgHeight) ,there's a huge memory leak each time i scroll a bit in the listview

当我使用await ImageLoader.DecodeSampledBitmapFromResourceAsync(localImageLocation,imgWidth,imgHeight)时,每次在listview中滚动一点时都会出现巨大的内存泄漏

I tried finding the references with the memory analyzer tool but there were none... Although MAT says that the problem are the bitmaps

我尝试使用内存分析器工具查找引用但没有...尽管MAT说问题是位图

public void ImageLoaded(string localImageLocation)
{
            int screenWidth = _activity.Resources.DisplayMetrics.WidthPixels;
            int imgWidth = screenWidth - (int)ConvertDpToPix (32f);
            int imgHeight = (int)(ConvertDpToPix(206f));

            BundleProgress.Visibility = ViewStates.Gone;

            if (CoverIv.Drawable != null)
            {
                            ((BitmapDrawable)CoverIv.Drawable).Bitmap.Recycle ();
                            ((BitmapDrawable)CoverIv.Drawable).Bitmap.Dispose ();
                            CoverIv.SetImageDrawable (null);
            }

            CoverIv.SetImageBitmap 
            (

            //MEMORYLEAK:   await ImageLoader.DecodeSampledBitmapFromResourceAsync  (localImageLocation, imgWidth, imgHeight)
                ((BitmapDrawable)_activity.Resources.GetDrawable(Resource.Drawable.splash)).Bitmap
            );

The methods in the ImageLoader class:

ImageLoader类中的方法:

        public static async Task DecodeSampledBitmapFromResourceAsync (String path,int reqWidth, int reqHeight)
            {

            // First decode with inJustDecodeBounds=true to check dimensions of the image
            BitmapFactory.Options optiOns= new BitmapFactory.Options();
            options.InJustDecodeBounds = true;
            Bitmap bitmap = await BitmapFactory.DecodeFileAsync(path, options);

            // Calculate inSampleSize
            options.InSampleSize = CalculateInSampleSize(options, reqWidth,
                reqHeight);

            // Decode bitmap with inSampleSize set
            options.InJustDecodeBounds = false;
            options.InPreferredCOnfig= Bitmap.Config.Argb8888;
            //options.InDither = true;

            return await BitmapFactory.DecodeFileAsync(path, options);
        }

      public static int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
        {
            // Raw height and width of image
            float height = options.OutHeight;
            float width = options.OutWidth;
            double inSampleSize = 1D;

            if (height > reqHeight || width > reqWidth)
            {
                int halfHeight = (int)(height / 2);
                int halfWidth = (int)(width / 2);

                // Calculate a inSampleSize that is a power of 2 - the decoder will use a value that is a power of two anyway.
                while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
                {
                    inSampleSize *= 2;
                }
            }

            return (int)inSampleSize;
        }

The adapter:

public override View GetView (int position, View convertView, ViewGroup parent)
    {
        BaseBundelVO bundle = _bundles [position];

        DSBundleListItem bundleHolder = null;
        DSBundleArchiveItem archiveHolder = null;

        int type = GetItemViewType(position);
        if (cOnvertView== null)
        {
            bundleHolder = new DSBundleListItem (_activity.ApplicationContext);
            archiveHolder = new DSBundleArchiveItem (_activity.ApplicationContext);

            switch (type) 
            {
            case 0:
                cOnvertView= _activity.LayoutInflater.Inflate (Resource.Layout.dsBundleListItem, null);
                bundleHolder.IcOnIv= convertView.FindViewById (Resource.Id.iconIv);
                bundleHolder.CoverIv = convertView.FindViewById (Resource.Id.coverIv);
                bundleHolder.CoverTitleTv = convertView.FindViewById (Resource.Id.coverTitleTv);
                bundleHolder.CoverSubTitleTv = convertView.FindViewById (Resource.Id.coverSubTitleTv);
                bundleHolder.BundleProgress = convertView.FindViewById (Resource.Id.bundleProgress);
                convertView.Tag = bundleHolder;
                break;

            case 1:
                cOnvertView= _activity.LayoutInflater.Inflate (Resource.Layout.dsBundleArchiveItem, null);
                archiveHolder.ArchiveTitleTv = convertView.FindViewById (Resource.Id.archiveTitleTv);
                archiveHolder.ArchiveSubTitleTv = convertView.FindViewById (Resource.Id.archiveSubTitleTv);
                convertView.Tag = archiveHolder;
                break;

            }

        } 
        else 
        {
            switch (type) 
            {
            case 0:
                bundleHolder = (DSBundleListItem)convertView.Tag;
            break;

            case 1:
                archiveHolder = (DSBundleArchiveItem)convertView.Tag;
            break;

            }
        }

        switch (type) 
        {
        case 0:
            bundleHolder.CoverTitleTv.Text = bundle.Title;
            bundleHolder.CoverSubTitleTv.Text = bundle.SubTitle;

            bundleHolder.CoverIv.SetImageDrawable (null);
            bundleHolder.IconIv.SetImageDrawable (null);

            bundleHolder.LoadImage(bundle.CoverImageLocation,bundle.Icon);
            break;

        case 1:
            archiveHolder.ArchiveTitleTv.Text    = "Archief";
            archiveHolder.ArchiveSubTitleTv.Text = "Bekijk onze eerder verschenen publicaties";
            break;

        }

        return convertView;
    }

    public void SetData(List bundles)
    {
        _bundles.Clear ();
        _bundles.AddRange(bundles);
    }

3 个解决方案

#1


0  

Use the picasso library when using images, it works wonders!

使用图像时使用毕加索库,它可以创造奇迹!

#2


0  

I recommend Pisasso library too. Here is the link: http://square.github.io/picasso/

我也推荐Pisasso图书馆。这是链接:http://square.github.io/picasso/

It retrieve images from the web immediately and caching it locally.

它立即从Web检索图像并在本地缓存它。

You must simply add this line to your code after importing Picasso library:

您必须在导入Picasso库后将此行添加到代码中:

Picasso.with(context).load("*your url*").into(*your image view*);

You can also load local images too!

您也可以加载本地图像!

Picasso.with(context).load("file:///android_asset/image.png").into(*your image view*);

Hope this will help,

希望这会有所帮助,

#3


0  

Please try my code. I think that it is something wrong with your decoding:

请试试我的代码。我认为你的解码有问题:

private Bitmap DisplayRotatedPhoto(Uri path) {
    String filePath = path.toString().substring(8);
    Bitmap oriented = null;
    BitmapFactory.Options bitmapFactoryOptiOns= new BitmapFactory.Options();
    bitmapFactoryOptions.inJustDecodeBounds = true;

    int REQUIRED_SIZE = 100;
    int width_tmp = bitmapFactoryOptions.outWidth, height_tmp = bitmapFactoryOptions.outHeight;
    int scale = 2;
    while (true) {
        if (width_tmp / 2 

推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文介绍了H5游戏性能优化和调试技巧,包括从问题表象出发进行优化、排除外部问题导致的卡顿、帧率设定、减少drawcall的方法、UI优化和图集渲染等八个理念。对于游戏程序员来说,解决游戏性能问题是一个关键的任务,本文提供了一些有用的参考价值。摘要长度为183字。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
  • Vagrant虚拟化工具的安装和使用教程
    本文介绍了Vagrant虚拟化工具的安装和使用教程。首先介绍了安装virtualBox和Vagrant的步骤。然后详细说明了Vagrant的安装和使用方法,包括如何检查安装是否成功。最后介绍了下载虚拟机镜像的步骤,以及Vagrant镜像网站的相关信息。 ... [详细]
author-avatar
小圈44
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有