Save image from url to sdcard
<p>I am saving image from url to sdcard. But image size is 0 in sdcard. Image is created in sdcard but now retrieve data from url and save. so it is giving me 0 size.</p>

<pre><code>try
   {
     URL url = new URL("http://api.androidhive.info/images/sample.jpg");
      InputStream input = url.openStream();
      try {
          //The sdcard directory e.g. '/sdcard' can be used directly, or 
          //more safely abstracted with getExternalStorageDirectory()
          File storagePath = Environment.getExternalStorageDirectory();
          OutputStream output = new FileOutputStream (new File(storagePath,username+".png"));
          try {
              byte[] buffer = new byte[2048];
              int bytesRead = 0;
              while ((bytesRead = input.read(buffer, 0, buffer.length)) &gt;= 0) {
                  output.write(buffer, 0, bytesRead);
              }
          } finally {
              output.close();
          }
      } finally {
          input.close();
      }
     } catch(Exception e)
     {
         System.out.println("error in sd card "+e.toString());
     } 
</code></pre>
