[英]android load image from url
I use the universal image loader
library to load about 50 image from url to listview
.我使用
universal image loader
库从 url 加载大约 50 个图像到listview
。 Here is the binderdata.这是活页夹数据。
public class BinderDataImg extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
public BinderDataImg() {
// TODO Auto-generated constructor stub
}
public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
this.imgHashmap = map;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return imgHashmap.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_img, null);
holder = new ViewHolder();
holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
String uri = imgHashmap .get(position).get(KEY_IMG);
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true).build();
imageLoader.displayImage(uri, holder.iv_img, options);
return vi;
}
static class ViewHolder{
ImageView iv_img;
}
}
The activity.活动。
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
Almost, but not all images load and in logcat there's an OutOfMemoryError
.几乎,但并非所有图像都加载,并且在 logcat 中有一个
OutOfMemoryError
。 How can this be fixed?如何解决这个问题?
Update:更新:
This is Binderdata from the Picasso
library.这是来自
Picasso
图书馆的 Binderdata。
public class BinderDataImg extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
public BinderDataImg() {
// TODO Auto-generated constructor stub
}
Activity act;
public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
this.imgHashmap = map;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.act = act;
}
public int getCount() {
// TODO Auto-generated method stub
return imgHashmap.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_img, null);
holder = new ViewHolder();
holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
String uri = imgHashmap .get(position).get(KEY_IMG);
Picasso.with(act).load(uri).into(holder.iv_img);
return vi;
}
static class ViewHolder{
ImageView iv_img;
}
}
This loads the images ok, but when scrolling up or down the images reload again.这可以正常加载图像,但是当向上或向下滚动时,图像会再次重新加载。
Ok here is my implemented code using picasso and its working smoothly for me好的,这是我使用毕加索实现的代码,它对我来说工作顺利
public class MyFragmentAdapter extends ArrayAdapter<Video> {
private Context mContext;
private LayoutInflater mInflater;
private Bitmap mBitmap;
public MyFragmentAdapter(Context context, int resource, List<Video> objects) {
super(context, resource, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.item_gallery, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.iv_thumbImage);
holder.pb = (ProgressBar) convertView.findViewById(R.id.iv_progressbar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.pb.setVisibility(View.VISIBLE);
String url="Url here";
Picasso.with(mContext).load(thumbnail_path).placeholder(R.drawable.icon_video).into(holder.imageview, new Callback() {
@Override
public void onSuccess() {
holder.pb.setVisibility(View.INVISIBLE);
}
@Override
public void onError() {
holder.pb.setVisibility(View.INVISIBLE);
}
});
}
return convertView;
}
public static class ViewHolder {
ImageView imageview;
ProgressBar pb;
}
}
Try with Aquery Image loading lib,that worked for me尝试使用 Aquery Image 加载库,这对我有用
public class CustomAdapterAccept extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,String>> listData;
private static final String TAG_NAME="brandname";
private static final String TAG_IMAGE="brandimg";
public CustomAdapterAccept(Context context,ArrayList<HashMap<String,String>> listData) {
this.context = context;
this.listData=listData;
aQuery = new AQuery(this.context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_cat, null);
holder.propic = (ImageView) convertView.findViewById(R.id.brndimage);
holder.txtproname = (TextView) convertView.findViewById(R.id.txtcatname);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtproname.setText(listData.get(position).get(TAG_NAME));
aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE), true, true, 0, R.drawable.ic_launcher);
aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
// image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
return convertView;
}
class ViewHolder{
ImageView propic;
TextView txtproname;
TextView txtproid;
TextView txtprofilecast;
TextView txtprofileage;
TextView txtprofileplace;
}
}
I use the universal image loader
library to load about 50 image from url to listview
.我使用
universal image loader
库从url加载到listview
约50张图像。 Here is the binderdata.这是活页夹数据。
public class BinderDataImg extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
public BinderDataImg() {
// TODO Auto-generated constructor stub
}
public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
this.imgHashmap = map;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return imgHashmap.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_img, null);
holder = new ViewHolder();
holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
String uri = imgHashmap .get(position).get(KEY_IMG);
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true).build();
imageLoader.displayImage(uri, holder.iv_img, options);
return vi;
}
static class ViewHolder{
ImageView iv_img;
}
}
The activity.活动。
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
Almost, but not all images load and in logcat there's an OutOfMemoryError
.几乎但不是全部图像都加载,并且在logcat中有一个
OutOfMemoryError
。 How can this be fixed?如何解决?
Update:更新:
This is Binderdata from the Picasso
library.这是来自
Picasso
库的Binderdata。
public class BinderDataImg extends BaseAdapter {
static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;
public BinderDataImg() {
// TODO Auto-generated constructor stub
}
Activity act;
public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
this.imgHashmap = map;
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.act = act;
}
public int getCount() {
// TODO Auto-generated method stub
return imgHashmap.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.list_img, null);
holder = new ViewHolder();
holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
String uri = imgHashmap .get(position).get(KEY_IMG);
Picasso.with(act).load(uri).into(holder.iv_img);
return vi;
}
static class ViewHolder{
ImageView iv_img;
}
}
This loads the images ok, but when scrolling up or down the images reload again.这样可以正常加载图像,但是在向上或向下滚动图像时会重新加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.