title: Android: how to wait AsyncTask to finish in MainThread?
body: <p>I know that the first you gonna this is... why the heck on the world you then use AsyncTask. </p>
  
  <p>So here is my problem i am working on some Android app (API 7 for android 2.1 or higher) , and i am testing on emulator and everything was fine, so then i tested on HTC Sensation and it says NetworkOnMainThreadExeption! </p>
  
  <p>I was downloading some pictures and then draw on the map.</p>
  
  <p>So to solve this problem every (internet connection) in this case downloading the pictures i must put on AsyncTask to work.</p>
  
  <p>So i need a method how to know when all pictures are done so i can start drawing.. </p>
  
  <p>I was trying so much and no result i have no idea. I got one solution with handler but if run on slower net i get nullpointer(because the pictures are not downloaded).</p>
  
  <p>So please help me. </p>
  
  <p>EDIT:</p>
  
  <p>here is the idea:</p>
  
  <pre><code>Bitmap bubbleIcon ;
      onCreate(){
       ...
  // i am making call for Async
  new ImgDown().execute(url);
  //and then i calling functions and classes to draw with that picture bubbleIcon !
  DrawOnMap(bubbleIcon);
  }
  
  
  
  
  //THIS IS ASYNC AND FOR EX. SUPPOSE I NEED TO DOWNLOAD THE PIC FIRST
       class ImgDown extends AsyncTask&lt;String, Void, Bitmap&gt; {
  
          private String url;
  
          public ImgDown() {
          }
  
          @Override
          protected Bitmap doInBackground(String... params) {
              url = params[0];
              try {
                  return getBitmapFromURL(url);
              } catch (Exception err) {
              }
  
              return null;
  
          }
  
          @Override
          protected void onPostExecute(Bitmap result) {
              bubbleIcon = result;
              bubbleIcon = Bitmap
                      .createScaledBitmap(bubbleIcon, 70, 70, true);
  
          }
  
          public Bitmap getBitmapFromURL(String src) {
              try {
                  Log.e("src", src);
                  URL url = new URL(src);
                  HttpURLConnection connection = (HttpURLConnection) url
                          .openConnection();
                  connection.setDoInput(true);
                  connection.connect();
                  InputStream input = connection.getInputStream();
                  // /tuka decode na slika vo pomalecuk kvalitet!
                  BitmapFactory.Options options = new BitmapFactory.Options();
                  options.inSampleSize = 3;
                  Bitmap myBitmap = BitmapFactory
                          .decodeStream(new FlushedInputStream(input));
                  Log.e("Bitmap", "returned");
                  return myBitmap;
              } catch (IOException e) {
                  e.printStackTrace();
                  Log.e("getBitmapFromURL", e.getMessage());
                  return null;
              }
          }
  
          class FlushedInputStream extends FilterInputStream {
              public FlushedInputStream(InputStream inputStream) {
                  super(inputStream);
              }
  
              public long skip(long n) throws IOException {
                  long totalBytesSkipped = 0L;
                  while (totalBytesSkipped &lt; n) {
                      long bytesSkipped = in.skip(n - totalBytesSkipped);
                      if (bytesSkipped == 0L) {
                          int byteValue = read();
                          if (byteValue &lt; 0) {
                              break; // we reached EOF
                          } else {
                              bytesSkipped = 1; // we read one byte
                          }
                      }
                      totalBytesSkipped += bytesSkipped;
                  }
                  return totalBytesSkipped;
              }
          }
      }
  </code></pre>
  
  <p>i hope now is more clear.</p>