0

I need to load an image from the web in my app, I found a good example here, but I can't figure out how to use the returned Bitmap in my Main Activity :

the class :

public class GetImageFromServer extends AsyncTask<Void, Void, Bitmap {
    private String sURL;

    GetImageFromServer(String urlParam) {
        sURL = urlParam;
    }

    @Override
    protected Bitmap doInBackground(Void... urlParam) {

        Bitmap bmp = null;

        //ImageView img = (ImageView) findViewById(R.id.imageView1);
        try {
            URL url = new URL(sURL);
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (Exception ex) {
            Log.println(1, "Profile:getImg", ex.getMessage());
        }
        return bmp;
    }

    @Override
    protected void onPreExecute() {}
}

And the MainActivity code :

String urlImage = "http://www.xxxxxx.com/css/images/xxxxxx.png";
GetImageFromServer gifs = new GetImageFromServer(urlImage);
gifs.execute();

if(person.has("Avatar")) {Avatar.setImageBitmap( gifs.execute())}

The error is :
gifs.execute()

Thanks for your help !

Add :

I added this "cancel(true)" because I have connection problems to JSON webservices after severals start/debug/close, but I doesn't seem to work :

@Override
protected Bitmap doInBackground(String... urlParam) {
    if (isCancelled())
        this.cancel(true);


    Bitmap b = null;........          

and

@Override
protected void onPostExecute(Bitmap result) {
    // use the result
    mImageView.setImageBitmap(result);
    this.cancel(true);
}

Could the assynctasks prevent my app to connect to my webservices ?

Alexus
  • 276
  • 1
  • 5
  • 22

2 Answers2

1

There is a good library for doing async image loadings, here is a link: https://github.com/square/picasso.

Or you could follow this approach:

public class LoadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {

private ImageView mImageView;

    public LoadImageAsyncTask(ImageView imageView) {
        mImageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(params[0]);
            bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());


        } catch (Exception ex) {
            Log.println(1, "Profile:getImg", ex.getMessage());
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        mImageView.setImageBitmap(bitmap);
    }
}

In your activity call:

LoadImageAsyncTask task = new LoadImageAsyncTask(findViewById(R.id.yourImageId)).execute(stringUrl);
gmetax
  • 3,853
  • 2
  • 31
  • 45
esthetic
  • 106
  • 5
  • Yes it works !! Thanks , just a little modif : GetImageFromServer task = (GetImageFromServer) new GetImageFromServer(Avatar).execute(urlImage); – Alexus Jul 09 '15 at 13:56
0

you have to implement a callback

your asynctask

public class GetImageFromServer extends AsyncTask<Void, Void, Bitmap> {

    private String sURL;
    private Bitmap b;
    YourCallback mOwner;

    GetImageFromServer(YourCallback owner, String urlParam) {
        sURL = urlParam;
        mOwner = owner;
    }

    @Override
    protected Bitmap doInBackground(Void... urlParam) {
        try {
            URL url = new URL(sURL);
            b = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());

        } catch (Exception ex) {
            Log.println(1, "Profile:getImg", ex.getMessage());
        }
        return b;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        // use the result
        super.onPostExecute(result);
        if (mOwner != null)
            mOwner.CallbackFunction(result);
    }

    public interface YourCallback {
        void CallbackFunction(Bitmap result);
    }
}

your MainActivity

public class MainActivity  extends Activity implements YourCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        GetImageFromServer gifs = new GetImageFromServer(urlImage, b);
        gifs.execute();
    }

    @Override
    public void CallbackFunction(Bitmap result) {
        if (person.has("Avatar")) {
            Avatar.setImageBitmap(result);
        }

    }

}
gmetax
  • 3,853
  • 2
  • 31
  • 45