gpt4 book ai didi

android - 从 URl Android 加载图像时出现内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:38 26 4
gpt4 key购买 nike

我目前正在使用 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;
}

Google Drive

它还包含 telerik dll 文件。

最佳答案

通过查看您在此处粘贴的代码 fragment ,另一个问题可能是“List()”每个都包含对“缩放”图像的引用 - 但这些似乎从未被发布/回收。

您是否尝试过使 PromotionClass 成为一次性产品?然后在 android Activity 的“OnDestroy()”方法中,您可以处理“PromotionClass”,这反过来可以回收/处理缩小的图像。

关于android - 从 URl Android 加载图像时出现内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com