gpt4 book ai didi

android - picasso 导致 OutOfMemory 错误

转载 作者:行者123 更新时间:2023-11-30 01:46:23 25 4
gpt4 key购买 nike

我正在使用 Picasso 将图片加载到网格中,但是每次我向下或向上滚动以加载图片时,它都会填满堆,以至于在 2-3 次滚动后内存不足。我做错了什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Json;
using System.Threading.Tasks;
using Android.App;
using Android.Support.V4.App;
using Android.Content;
using Android.OS;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using *******.Adapters;
using *******.Models;
using Newtonsoft.Json;
using Com.Squareup.Picasso;
using System.Net;
using System.IO;

namespace *******.Fragments {
public class Gallery : Android.Support.V4.App.Fragment {
private GridView gridView;
private ISharedPreferences pref;
private SessionManager session;
private DeviceModel deviceModel;
public static Activity activity;
private ProgressDialog progressDialog;
private string cookie;
private string sensorId;
private List<string> photos;
private const string URL_DATA = "http://*******/appapi/getdata";

public override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);

activity = Activity;
session = new SessionManager();
pref = Activity.GetSharedPreferences("UserSession", FileCreationMode.Private);
cookie = pref.GetString("PHPSESSID", string.Empty);
sensorId = Arguments.GetString("id");
photos = new List<string>();
}

public async override void OnStart() {
base.OnStart();

// progressDialog = ProgressDialog.Show(activity, String.Empty, GetString(Resource.String.loading_text));
// progressDialog.Window.ClearFlags(WindowManagerFlags.DimBehind);

await GetSensorData();
await GetPhotoUrls();

gridView.Adapter = new ImageAdapter(activity, photos);

gridView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
Toast.MakeText(Activity, e.Position.ToString(), ToastLength.Short).Show();
};

// progressDialog.Hide();
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.Inflate(Resource.Layout.Gallery, container, false);

gridView = view.FindViewById<GridView>(Resource.Id.gridview);

return view;
}

public async Task GetSensorData() {
var jsonFetcher = new JsonFetcher();
JsonValue jsonGalerry = await jsonFetcher.FetchSensorDataAsync(URL_DATA, sensorId, "DESC", "20", cookie);
deviceModel = new DeviceModel();
deviceModel = JsonConvert.DeserializeObject<DeviceModel>(jsonGalerry);
}

public async Task GetPhotoUrls() {
foreach (var data in deviceModel.SensorData) {
string url = "*******" + data.StringData;
Console.WriteLine(url);
// Bitmap photoBitmap = await new ImageDownloader().GetImageBitmapFromUrlAsync(url, Activity, 300, 300);
// Drawable photoDrawable = new BitmapDrawable(Resources, photoBitmap);
photos.Add(url);
}
}
}

public class ImageAdapter : BaseAdapter {
Context context;
List<string> photos;

public ImageAdapter(Context context, List<string> photos) {
this.context = context;
this.photos = photos;
}

public override int Count {
get { return photos.Count; }
}

public override Java.Lang.Object GetItem(int position) {
return null;
}

public override long GetItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public override View GetView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(context);
// imageView.LayoutParameters = new GridView.LayoutParams (500, 500);
// imageView.SetScaleType (ImageView.ScaleType.CenterCrop);
// imageView.SetPadding (8, 8, 8, 8);
} else {
imageView = (ImageView)convertView;
}

var imageDownloader = new ImageDownloader();
imageDownloader.DownloadImage(photos[position], context, imageView);

return imageView;
}
}
}

下载图片方法:

    public void DownloadImage(string url, Context context, ImageView imageView) {
var picasso = new Picasso.Builder(context).Downloader(new PhotoDownloader(context)).Build();
picasso.Load(url).Into(imageView);
}

private class PhotoDownloader : UrlConnectionDownloader {
private Context context;

public PhotoDownloader(Context context) : base(context) {
this.context = context;
}

protected override HttpURLConnection OpenConnection(Android.Net.Uri uri) {
HttpURLConnection conn = base.OpenConnection(uri);
ISharedPreferences pref = context.GetSharedPreferences("UserSession", FileCreationMode.Private);
string cookie = pref.GetString("PHPSESSID", string.Empty);

conn.SetRequestProperty("Cookie", "PHPSESSID=" + cookie);

return conn;
}
}

最佳答案

是的,gropapa's comment是正确的:如果您构建一个以上的新 Picasso 实例,它们中的每一个都会有一个单独的缓存,您的应用程序将很快耗尽内存。

相反,我只会构建实例一次(可能在 ApplicationonCreate 中),然后使用 Picasso.setSingletonInstance 来存储该实例供以后使用。从那时起,Picasso.with 将始终返回您创建的实例。请注意,您必须在第一次调用 Picasso.with() 之前执行此操作(否则 Picasso 将在第一次调用时创建自己的实例,如果您稍后调用 setSingletonInstance)。

关于android - picasso 导致 OutOfMemory 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658072/

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