2

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);
                    }

}
}
2
  • Please show us where and how you are using this AsyncTask (probaly in your list adapter code ?) The mistake may be in the getView method from the Adapter... Commented Jul 18, 2013 at 9:56
  • @Guian getview method is added Commented Jul 18, 2013 at 10:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your help. I have modified as your answer and also added ((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(null); in the preexecute method of asyntask. Now the first listitem shows wrong images foe 2 or 3 seconds then shows the correct one.
You need to find where those wrong images were set at first. Check yur layout xml for the item row, do you set a picture there ?
Yes a transparent image is there in the layout
Wrong image means next list item images
try to 'unplug' the cache mechanism by commenting the else lines and see if it comes from a bad picture in cache.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.