0

I am currently working on using asynctask to load image. I have reference to a example class . However, the Bitmap result there is null. Why is that and how can I fix the problem ? thanks. The code are shown below.

package com.example.json;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

class ImgAdapter extends AsyncTask<Object, Void, Bitmap> {

    private ImageView imv;
    private String path;

    public ImgAdapter(ImageView imv) {
        this.imv = imv;
        this.path = imv.getTag().toString();
    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        Bitmap bitmap = null;
        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + path);

        if (file.exists()) {
            bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (!imv.getTag().toString().equals(path)) {
            /*
             * The path is not same. This means that this image view is handled
             * by some other async task. We don't do anything and return.
             */
            return;
        }

        if (result != null && imv != null) {
            Log.i("test","success");
            imv.setVisibility(View.VISIBLE);
            imv.setImageBitmap(result);
        } else {
            Log.i("test","result=" + String.valueOf(result == null)); //result is null here
            Log.i("test","imv=" + String.valueOf(imv == null));
            Log.i("test","fail");
            imv.setVisibility(View.GONE);
        }
    }
}

How to call in ListView adapter:

public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView thumbnail = (ImageView) arg1.findViewById(R.id.imageView1);
ShopEntry entry = getItem(arg0);
thumbnail.setTag(entry.image_url);
new ImgAdapter(thumbnail).execute();
return arg1;
}
user782104
  • 13,233
  • 55
  • 172
  • 312
  • 1
    Check this link. http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – Ahmad Raza Jul 30 '13 at 02:09
  • 1
    Instead of passing imageview to the task, you just need to fill your dataset in the adapter with the returned image and then in getView change images accordingly. Above implementation is buggy – Pulkit Sethi Jul 30 '13 at 02:26
  • Do you mean I should set the URL to imageview in my listview adapter? the function of asynctask class is only to return an update URL? would You mind provide an example, thanks. – user782104 Jul 30 '13 at 02:34
  • I'm doing something similar HERE!!!http://stackoverflow.com/questions/18808114/picasso-loading-of-image-spawned-inside-asynctask – Etienne Lawlor Sep 15 '13 at 01:54
  • You may want to see my own answer to my question:https://stackoverflow.com/questions/44216331/image-thumbnails-not-setting-correctly/44528936#44528936 – Pushan Gupta Jun 13 '17 at 18:31

1 Answers1

1

change this:

Bitmap bitmap = null;
        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + path);

        if (file.exists()) {
            bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        }

to this:

 Bitmap bitmap = ((BitmapDrawable)imv.getDrawable()).getBitmap();
fenix
  • 1,716
  • 2
  • 20
  • 26