"Out of memory"使用Picasso下载图片并将图片放入GridView时
"Out of memory" when using Picasso to download images and put images into GridView
我有一个应用程序可以将某些图像加载到网格视图中。问题是,加载 2 或 3 个图像后出现以下错误:
E/dalvikvm-heap﹕ Out of memory on a 7056016-byte allocation.
我已经阅读了其他与 Whosebug 相关的问题,一些解决方案包括在位图上使用 .fit() 和 .resize() 来解决这个问题。
问题是,我正在直接从 URL 下载图像。
你能建议我解决这个 OOM 问题的方法吗?
干杯!
这是我的 GridView 适配器代码:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
List<ParseObject> urlList;
private LayoutInflater inflater;
public ImageAdapter(Context c, List<ParseObject> urlList) {
mContext = c;
this.urlList = urlList;
inflater = LayoutInflater.from(c);
}
public int getCount() {
return urlList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View view, ViewGroup parent) {
View gridItem = view;
ViewHolder viewHolder;
if (gridItem == null) {
viewHolder = new ViewHolder();
gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
viewHolder.squareImageView = (SquareImageView) gridItem.findViewById(R.id.gridItem_squareImageView);
viewHolder.progressBar = (ProgressBar) gridItem.findViewById(R.id.gridItem_progressBar);
gridItem.setTag(viewHolder);
} else {
viewHolder= (ViewHolder) view.getTag();
}
Picasso.with(mContext).load(urlList.get(position).get("thumbnailURL").toString()).into(viewHolder.squareImageView);
viewHolder.progressBar.setVisibility(View.INVISIBLE);
return gridItem;
}
class ViewHolder {
SquareImageView squareImageView;
ProgressBar progressBar;
}
}
试试这个,它会使用更少的内存。在加载图像之前它会调整它的大小
Picasso.with(mContext).
load(urlList.get(position).get("thumbnailURL").toString())
.resize(100,100)
.into(viewHolder.squareImageView);
我有一个应用程序可以将某些图像加载到网格视图中。问题是,加载 2 或 3 个图像后出现以下错误:
E/dalvikvm-heap﹕ Out of memory on a 7056016-byte allocation.
我已经阅读了其他与 Whosebug 相关的问题,一些解决方案包括在位图上使用 .fit() 和 .resize() 来解决这个问题。
问题是,我正在直接从 URL 下载图像。
你能建议我解决这个 OOM 问题的方法吗?
干杯!
这是我的 GridView 适配器代码:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
List<ParseObject> urlList;
private LayoutInflater inflater;
public ImageAdapter(Context c, List<ParseObject> urlList) {
mContext = c;
this.urlList = urlList;
inflater = LayoutInflater.from(c);
}
public int getCount() {
return urlList.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View view, ViewGroup parent) {
View gridItem = view;
ViewHolder viewHolder;
if (gridItem == null) {
viewHolder = new ViewHolder();
gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
viewHolder.squareImageView = (SquareImageView) gridItem.findViewById(R.id.gridItem_squareImageView);
viewHolder.progressBar = (ProgressBar) gridItem.findViewById(R.id.gridItem_progressBar);
gridItem.setTag(viewHolder);
} else {
viewHolder= (ViewHolder) view.getTag();
}
Picasso.with(mContext).load(urlList.get(position).get("thumbnailURL").toString()).into(viewHolder.squareImageView);
viewHolder.progressBar.setVisibility(View.INVISIBLE);
return gridItem;
}
class ViewHolder {
SquareImageView squareImageView;
ProgressBar progressBar;
}
}
试试这个,它会使用更少的内存。在加载图像之前它会调整它的大小
Picasso.with(mContext).
load(urlList.get(position).get("thumbnailURL").toString())
.resize(100,100)
.into(viewHolder.squareImageView);