Reputation: 1561
In my Android application an Asyntask is used for loading images from web in a list view.I want to show the center portion of the image and I have tried imageloader for loading images but the problem is image clarity is very poor but I have used it for loading thumbnails because thumbnail is very small.So that I have tried an Asyntask which is given below.The problem with this method is image view shows other images that means image view in first list item will show the next list item's image and after some time it will display the correct image.How can I solve this issue.Or suggest a method for lading images with good clarity.Please help me.Thanks in advance
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
ProgressBar mProgressBar;
String url;
public DownloadImageTask(ImageView bmImage, ProgressBar progressBar) {
this.bmImage = bmImage;
this.mProgressBar = progressBar;
}
@Override
protected void onPreExecute() {
mProgressBar.setVisibility(View.VISIBLE);
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
url = urldisplay;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
// Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
Utilities.addBitmapToMemoryCache(url, result);
bmImage.setImageBitmap(result);
mProgressBar.setVisibility(View.GONE);
// result.recycle();
}
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
vi = convertView;
int type = getItemViewType(position);
// Utilities.message_player = new MediaPlayer();
if (vi == null) {
inflater = LayoutInflater.from(mcontext);
if (type == ITEM_TYPE_ONE)
vi = inflater.inflate(R.layout.message_group_list_item, null);
else
vi = inflater.inflate(R.layout.listlastrow, null);
}
if (type == ITEM_TYPE_ONE) {
if (fontType == true) {
}
((ImageView) vi.findViewById(R.id.imageMessage)).setTag(position+"i");
Bitmap bmp = Utilities.getBitmapFromMemCache(feedsImage);
if(bmp == null){
DownloadImageTask dwnloadImgTask = new DownloadImageTask(((ImageView) vi.findViewWithTag(position+"i")),
((ProgressBar) vi.findViewById(R.id.progressBar1)));
dwnloadImgTask.execute(feedsImage);
}else{
((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(bmp);
}
}
}
Upvotes: 2
Views: 1998
Reputation: 4689
When you are setting the bitmap from cache or downloading a new image, you are missing something :
Your parent view (vi) is possibly a previously created view recycled to avoid recreating each views (that what convertView is for).
This convertView has already an image on it since it's a recycled item for which an image has been loaded.
So you have to reset the image view content in case you've got a convertView :
((ImageView) vi.findViewById(R.id.imageMessage)).setTag(position+"i");
Bitmap bmp = Utilities.getBitmapFromMemCache(feedsImage);
if(bmp == null){
//HERE I add a line to reset the bitmap of the imageview
((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(null);
DownloadImageTask dwnloadImgTask = new DownloadImageTask(((ImageView) vi.findViewWithTag(position+"i")), ((ProgressBar) vi.findViewById(R.id.progressBar1)));
dwnloadImgTask.execute(feedsImage);
}else{
((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(bmp);
}
I've set null
to remove the bitmap, but you probably want to set a placeholder bitmap ( like a grey picture with a reload icon on it or something similar... )
Hope that helps.
Upvotes: 2