本文介绍了OutOfMemoryError在我的gridAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为我的gridView创建适配器,该适配器应该包含我在图库中选择的照片。但是我有以下在线 返回行
的OutOfMemoryError
公共类GridViewAdapter延伸ArrayAdapter<字符串> {
私人上下文上下文;
private int layoutResourceId;
private ArrayList< String> images_urls;
public GridViewAdapter(Context context,int layoutResourceId,ArrayList< String> images_urls){
super(context,layoutResourceId,images_urls);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.images_urls = images_urls;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
View row = convertView;
ViewHolder holder = null;
if(row == null){
LayoutInflater inflater =((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId,parent,false);
holder = new ViewHolder();
holder.image =(ImageView)row.findViewById(R.id.grid_image);
row.setTag(持有者);
} else {
holder =(ViewHolder)row.getTag();
}
文件f =新文件(images_urls.get(position));
Uri imageUri = Uri.fromFile(f);
holder.image.setImageURI(imageUri);
返回行;
}
静态类ViewHolder {
ImageView image;
}
}
我只是不知道是什么导致了错误。有人可以帮我解决这个问题吗?
解决方案如果您有兴趣,图像加载是一种更复杂的方法它有很多话题/博客/等您可以在线阅读。但是StackOverflow是一个直接的问题答案,所以,为了解决这个问题,导入这个库:
然后替换这些行:
File f = new File(images_urls.get(position));
Uri imageUri = Uri.fromFile(f);
holder.image.setImageURI(imageUri);
附带:
Picasso
.with(holder.image.getContext())
.load(image_urls.get(position))$ b $ .into(holder.image);
该库将负责正确管理内存。请确保进一步检查库API,因为您可以在屏幕上放置额外的命令来调整图像大小,以便使用更少的内存。
I'm trying create adapter for my gridView which should contains photo, which I chose in gallery. But I have following OutOfMemoryError in line "return row"
public class GridViewAdapter extends ArrayAdapter<String> {
private Context context;
private int layoutResourceId;
private ArrayList<String> images_urls;
public GridViewAdapter(Context context, int layoutResourceId, ArrayList<String> images_urls) {
super(context, layoutResourceId, images_urls);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.images_urls = images_urls;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.grid_image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
File f = new File(images_urls.get(position));
Uri imageUri = Uri.fromFile(f);
holder.image.setImageURI(imageUri);
return row;
}
static class ViewHolder {
ImageView image;
}
}
And I just don't know what makes error. Could someone help me solve it?
解决方案
Image loading is a way way more complex thema than this, if you're interested on it, there's lots of topics/blog/etc you can read online. But StackOverflow is a direct question-answer, so,
To fix the issue, import this library: https://github.com/square/picasso
and then replace those lines:
File f = new File(images_urls.get(position));
Uri imageUri = Uri.fromFile(f);
holder.image.setImageURI(imageUri);
with:
Picasso
.with(holder.image.getContext())
.load(image_urls.get(position))
.into(holder.image);
this library will take care of proper managing the memory. Make sure to further check the library API as you can put extra commands to resize the image on-screen so it uses less memory.
这篇关于OutOfMemoryError在我的gridAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!