title: Using AsyncTask, android
body: <p>I'm trying to get an image from a server and use that file as the background fro my app.
  Ive already leared that I should use AsyncTask to do so but my app still crashes or freezes when I run it.</p>
  
  <p>Heres the code I use:</p>
  
  <p>To call the AsyncTask:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Intent BG = new Intent((Intent) DownloadBGTask.THREAD_POOL_EXECUTOR);
  </code></pre>
  
  <p>The AsyncTask:</p>
  
  <pre><code>import java.net.URL;
  
  import com.pxr.tutorial.json.Getbackground;
  
  import android.os.AsyncTask;
  
  public class DownloadBGTask extends AsyncTask&lt;URL, Integer, Long&gt; {
      protected Long doInBackground(URL... urls) {
          int count = urls.length;
          long totalSize = 0;
          for (int i = 0; i &lt; count; i++) {
              totalSize += Getbackground.downloadFile(urls[i]);
              publishProgress((int) ((i / (float) count) * 100));
              // Escape early if cancel() is called
              if (isCancelled()) break;
          }
          return totalSize;
      }
  
      protected void onProgressUpdate(Integer... progress) {
          // Things to be done while execution of long running operation
      }
  
      protected void onPostExecute(Long result) {
          // xecution of result of Long time consuming operation
      }
  }
  </code></pre>
  
  <p>And the Getbackground.java:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  import android.os.Environment;
  
  public class Getbackground {
      URL url;
  
      public static long downloadFile(URL url2) {
      try {
  
          URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
          InputStream input = url.openStream();{
          try {
  
              File fileOnSD=Environment.getExternalStorageDirectory();    
              String storagePath = fileOnSD.getAbsolutePath();
              OutputStream output = new FileOutputStream (storagePath + "/oranjelangbg.png");
                  try {
  
                      byte[] buffer = new byte[1000000];
                      int bytesRead = 0;
                      while ((bytesRead = input.read(buffer, 0, buffer.length)) &gt;= 0) {
                      output.write(buffer, 0, bytesRead);
                      }
                      } finally {
              output.close();
              }
              } catch (IOException e) {
              throw new RuntimeException(e);
      } finally {
          try {
              input.close();
              } catch (IOException e) {
              throw new RuntimeException(e);
       }
     }    
   }    
          } catch (MalformedURLException ex) {
           throw new RuntimeException(ex);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
  
          return 0;
      }
  }
  </code></pre>
  
  <p>PS, Sorry for the crappy code, I'm really new to this so dont be supprised if there are some really stupid misakes in there.</p>