我目前正在使用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文件。