我目前正在使用Xamarin。開發我的Android應用程序 我遇到的問題是當我加載一堆圖像時,它需要我的堆棧內存的一大塊,並且不能正確重置。因此,如果應用程序在Activity A上啓動,並且轉到Activity B(加載它的圖像),我會返回到Activity A並返回到B它崩潰(內存不足問題)。我上傳了一個演示應用程序,演示了這個問題正在經歷着什麼。從URl Android加載圖像時獲取內存泄漏
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文件。
我已添加代碼 –
我只需使用UrlImageViewHelper爲您處理所有這些問題:https://components.xamarin.com/view/urlimageviewhelper這是一個了不起的庫。或者查看picasso:https://components.xamarin.com/view/square.picasso – JamesMontemagno