0

Okay, it might be a noob question, but I couldnt find anything to help me. I am loading an BitMap on a ImageView as an Async task, in this class:

class MyNetworkTask extends AsyncTask<URL, Void, Bitmap> {

ImageView tIV;

public MyNetworkTask(ImageView iv){
    tIV = iv;
}

@Override
protected Bitmap doInBackground(URL... urls) {

    Bitmap networkBitmap = null;

    URL networkUrl = urls[0]; //Load the first element
    try {
        networkBitmap = BitmapFactory.decodeStream(
                networkUrl.openConnection().getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return networkBitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
    tIV.setImageBitmap(result);
}

And thats how I call it from my Activity:

new MyNetworkTask(myImageView).execute(photo_url);

But that is going to be async, right? Which means that if right after in my Activity I try to use the BitMap, it will be null.

Is there a loop like a "do when??" that waits for this BitMap, and than do something?

Or is there a way of handling a callback from that Async Class?

Lucas Storti
  • 88
  • 1
  • 10
  • 1
    *is there a way of handling a callback from that Async Class* yes it's called a callback. If you google for 'android asynctask callback' you will get about 23563465467 results explaning how it works :-) – Tim Jun 03 '16 at 15:15
  • Thanks @TimCastelijns, I will take a look at this link :) – Lucas Storti Jun 03 '16 at 15:29
  • If you feel that the link answers your question, you can click the banner up top to accept it as a duplicate :-) – Tim Jun 03 '16 at 15:32
  • @TimCastelijns - Thanks! – Lucas Storti Jun 03 '16 at 16:02

3 Answers3

0

If you are speaking of storing the bitmap so you can access it later you should create a public Bitmap bitmap; outside of your AsyncTask then populate it from your AsyncTask.

class MyNetworkTask extends AsyncTask<URL, Void, Bitmap> {

ImageView tIV;

public MyNetworkTask(ImageView iv){
    tIV = iv;
}

@Override
protected Bitmap doInBackground(URL... urls) {

    Bitmap networkBitmap = null;

    URL networkUrl = urls[0]; //Load the first element
    try {
        networkBitmap = BitmapFactory.decodeStream(
                networkUrl.openConnection().getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    return networkBitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
    if(result != null){
        bitmap = result;
    }
    tIV.setImageBitmap(result);
}
philip
  • 1,292
  • 3
  • 24
  • 44
0

You can use the pattern observer for that. Check out this link for the implementation http://www.tutorialspoint.com/design_pattern/observer_pattern.htm

thushcapone
  • 162
  • 5
-1

What do you mean with "do when"? I think of something like that:

do{
    networkBitmap = BitmapFactory.decodeStream(
            networkUrl.openConnection().getInputStream());
}while(networkBitmap==null)

same works with a while loop:

while(networkBitmap==null){
    networkBitmap = BitmapFactory.decodeStream(
            networkUrl.openConnection().getInputStream());
}
purpule
  • 116
  • 1
  • 10
  • No that is not what he means – Tim Jun 03 '16 at 15:18
  • Although one is a pretest loop and one is a posttest loop – Logan Jun 03 '16 at 15:19
  • yeah, but he asked also for something like a "do when" loop, which basically is a while- or do-while-loop. i know, that there won't happen anything usefull in my loops, but with such loops he could anyhow try to get a bitmap, while it is null – purpule Jun 03 '16 at 15:22
  • Do note that "while" and "when" are very very different things, especially in programming – Tim Jun 03 '16 at 15:24
  • Thanks man, but it is not a do while, that I know how to do :) . I dont even know if there is such a thing as a "do when", thats why I asked. :) – Lucas Storti Jun 03 '16 at 15:28