从URL加载图片时遇到内存泄漏问题(Android)

4

我目前正在使用Xamarin来开发我的安卓应用程序。我遇到的问题是当我加载大量图片时,它会占用大量的堆栈内存,并且不能正确地重置。因此,如果应用程序从Activity A开始,然后转到Activity B(加载图片),我返回到Activity A并返回到B,它就会崩溃(内存不足问题)。我上传了一个演示应用程序,展示了我所遇到的问题。

namespace imageLoader
{
[Activity (Label = "LoadImages")]           
public class LoadImages : Activity
{
    Bitmap image;
    List<PromotionClass> pro;
    PromotionAdapter proAdapter;
    RadListView radlist;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.LoadImages);
        LinearLayout line = (LinearLayout)FindViewById   (Resource.Id.lin);
         radlist = new RadListView (this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,1, 
            LinearLayoutManager.Horizontal, false);
        radlist.SetLayoutManager (gridLayoutManager);
        pro = new List<PromotionClass>();
        ArrayList a = new ArrayList ();
        a.Add ("http://apk.payment24.co.za/promotions/nov/Zappar.jpg");
        a.Add ("http://apk.payment24.co.za/promotions/nov/Valpre.jpg");
        a.Add ("http://apk.payment24.co.za/promotions/nov/Tropika.jpg");
        int dent = (int)Resources.DisplayMetrics.Density;
        foreach (var url in a) {
            image = GlobalMethods.GetImageBitmapFromUrl(url.ToString(),dent);
            pro.Add(new PromotionClass("","",image));
        }
        proAdapter = new PromotionAdapter (pro, this);
        radlist.SetAdapter (proAdapter);
        line.AddView (radlist);
        // Create your application here
    }
    public override void OnBackPressed ()
    {

        SetContentView (Resource.Layout.Main);
        Intent intent = new Intent (this, typeof(LoadImages));
        intent.AddFlags (ActivityFlags.NewTask);
        intent.AddFlags (ActivityFlags.ClearTask);
        intent.AddFlags (ActivityFlags.NoAnimation);
        StartActivity (intent);
        this.Finish ();
    }
}

}

public  static Bitmap GetImageBitmapFromUrl(string url,int dens)
    {
        Bitmap bitmapScaled = null;
        using (var webClient = new WebClient())
        {
            var imageBytes = webClient.DownloadData(url);
            if (imageBytes != null && imageBytes.Length > 0)
            {
                // Create an image from the Byte Array
                Bitmap imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);

                bitmapScaled = Bitmap.CreateScaledBitmap(imageBitmap, imageBitmap.Height * dens, imageBitmap.Width * dens, true);
                imageBitmap.Recycle();
            }
        }

        // Return the new Scaled image 
        return bitmapScaled;
    }

谷歌云端硬盘中还包含telerik的dll文件。


我已经添加了代码。 - Charl Potgieter
我会简单地使用UrlImageViewHelper为您处理所有这些内容:https://components.xamarin.com/view/urlimageviewhelper 这是一个很棒的库。或者查看picasso:https://components.xamarin.com/view/square.picasso - JamesMontemagno
2个回答

0
从您在此处粘贴的代码片段来看,另一个问题可能是每个“List()”都保存了对“scaled”图像的引用,但这些图像似乎从未被释放/回收。
您是否尝试过使PromotionClass成为可处理的?然后在android活动的“OnDestroy()”方法中,您可以处理“PromotionClass”,这将依次回收/处理缩小的图像。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接