gpt4 book ai didi

java - 无法在 RemoteViewsFactory 中加载多个图像

转载 作者:行者123 更新时间:2023-11-30 09:15:10 25 4
gpt4 key购买 nike

我正在创建一个简单的 Android 小部件,用于从网站获取并显示一些电影封面。显示图像的 GridView 很简单。该小部件工作正常,但当我开始滚动时,我得到了 OutOfMemoryError 并且 Android 终止了我的进程。

这是我的RemoteViewsFactory 的代码。我似乎无法理解如何解决这个问题。我使用的图像大小合适,因此我不会在 Android 中浪费内存。它们尺寸完美。如您所见,我使用了一个非常小的 LRU 缓存,并且在我的 list 中,我什至启用了 android:largeHeap="true"。我已经为这个问题绞尽脑汁整整两天了,我想知道我正在尝试做的事情是否可行?

public class SlideFactory implements RemoteViewsFactory {

private JSONArray jsoMovies = new JSONArray();
private Context ctxContext;
private Integer intInstance;
private RemoteViews remView;
private LruCache<String, Bitmap> lruCache;
private static String strCouch = "http://192.168.1.110:5984/movies/";

public SlideFactory(Context ctxContext, Intent ittIntent) {
this.ctxContext = ctxContext;
this.intInstance = ittIntent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
this.lruCache = new LruCache<String, Bitmap>(4 * 1024 * 1024);
final Integer intMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final Integer intBuffer = intMemory / 8;

lruCache = new LruCache<String, Bitmap>(intBuffer) {
@Override
protected int sizeOf(String strDigest, Bitmap bmpCover) {
return bmpCover.getByteCount() / 1024;
}
};
}

public RemoteViews getViewAt(int intPosition) {
if (intPosition <= getCount()) {
try {
String strDocid = this.jsoMovies.getJSONObject(intPosition).getString("id");
String strDigest = this.jsoMovies.getJSONObject(intPosition).getJSONObject("value").getJSONObject("_attachments").getJSONObject("thumb.jpg").getString("digest");
String strTitle = this.jsoMovies.getJSONObject(intPosition).getJSONObject("value").getString("title");
Bitmap bmpThumb = this.lruCache.get(strDigest);

if (bmpThumb == null) {
String strUrl = strCouch + strDocid + "/thumb.jpg";
System.out.println("Fetching" + intPosition);
bmpThumb = new ImageFetcher().execute(strUrl).get();
this.lruCache.put(strDigest, bmpThumb);
}
remView.setImageViewBitmap(R.id.movie_cover, bmpThumb);
remView.setTextViewText(R.id.movie_title, strTitle);
} catch (Exception e) {
e.printStackTrace();
}
return remView;
}
return null;
}

public void onCreate() {
return;
}

public void onDestroy() {
jsoMovies = null;
}

public int getCount() {
return 20;
}

public RemoteViews getLoadingView() {
return null;//new RemoteViews(this.ctxContext.getPackageName(), R.layout.loading);
}

public int getViewTypeCount() {
return 1;
}

public long getItemId(int intPosition) {
return intPosition;
}

public boolean hasStableIds() {
return true;
}

public void onDataSetChanged() {
this.remView = new RemoteViews(this.ctxContext.getPackageName(), R.layout.slide);
try {
DefaultHttpClient dhcNetwork = new DefaultHttpClient();
String strUrl = strCouch + "_design/application/_view/language?" + URLEncoder.encode("descending=true&startkey=[\"hi\", {}]&attachments=true");
HttpGet getMovies = new HttpGet(strUrl);
HttpResponse resMovies = dhcNetwork.execute(getMovies);
Integer intMovies = resMovies.getStatusLine().getStatusCode();
if (intMovies != HttpStatus.SC_OK) {
throw new HttpResponseException(intMovies, "Server responded with an error");
}
String strMovies = EntityUtils.toString(resMovies.getEntity(), "UTF-8");
this.jsoMovies = new JSONObject(strMovies).getJSONArray("rows");
} catch (Exception e) {
Log.e("SlideFactory", "Unknown error encountered", e);
}
}
}

这是获取图像的 AsyncTask 的源代码:

public class ImageFetcher extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... strUrl) {
Bitmap bmpThumb = null;
try {
URL urlThumb = new URL(strUrl[0]);
HttpURLConnection hucConnection = (HttpURLConnection) urlThumb.openConnection();
InputStream istThumb = hucConnection.getInputStream();
bmpThumb = BitmapFactory.decodeStream(istThumb);
istThumb.close();
hucConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return bmpThumb;
}
}

最佳答案

我有过类似的痛苦经历,经过大量挖掘,我发现 setImageViewBitmap 将所有位图复制到新实例中,因此占用了双倍内存。考虑将以下行更改为静态资源或其他内容!

remView.setImageViewBitmap(R.id.movie_cover, bmpThumb);

这需要大量内存并 ping 垃圾收集器来清理内存,但速度太慢以至于您的应用无法及时使用释放的内存。

关于java - 无法在 RemoteViewsFactory 中加载多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20055118/

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