public static File downloadBitmapToFile(Context context,String urlString,String uniqueName){
  final File cacheDir=ImageCache.getDiskCacheDir(context,uniqueName);
  if (!cacheDir.exists()) {
    cacheDir.mkdir();
  }
  if (BuildConfig.DEBUG) {
    Log.d(TAG,"downloadBitmap - downloading - " + urlString);
  }
  HttpURLConnection urlConnection=null;
  BufferedOutputStream out=null;
  try {
    final File tempFile=File.createTempFile("bitmap",null,cacheDir);
    final URL url=new URL(urlString);
    urlConnection=(HttpURLConnection)url.openConnection();
    if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
      return null;
    }
    final InputStream in=new BufferedInputStream(urlConnection.getInputStream(),IO_BUFFER_SIZE_BYTES);
    out=new BufferedOutputStream(new FileOutputStream(tempFile),IO_BUFFER_SIZE_BYTES);
    int b;
    while ((b=in.read()) != -1) {
      out.write(b);
    }
    return tempFile;
  }
 catch (  final IOException e) {
    Log.e(TAG,"Error in downloadBitmap - " + e);
  }
 finally {
    if (urlConnection != null) {
      urlConnection.disconnect();
    }
    if (out != null) {
      try {
        out.close();
      }
 catch (      final IOException e) {
        Log.e(TAG,"Error in downloadBitmap - " + e);
      }
    }
  }
  return null;
}

public static File downloadBitmapToFile ( Context context , String urlString , String uniqueName ) { final File cacheDir = ImageCache . getDiskCacheDir ( context , uniqueName ) ; if ( ! cacheDir . exists ( ) ) { cacheDir . mkdir ( ) ; } if ( BuildConfig . DEBUG ) { Log . d ( TAG , "downloadBitmap - downloading - " + urlString ) ; } HttpURLConnection urlConnection = null ; BufferedOutputStream out = null ; try { final File tempFile = File . createTempFile ( "bitmap" , null , cacheDir ) ; final URL url = new URL ( urlString ) ; urlConnection = ( HttpURLConnection ) url . openConnection ( ) ; if ( urlConnection . getResponseCode ( ) != HttpURLConnection . HTTP_OK ) { return null ; } final InputStream in = new BufferedInputStream ( urlConnection . getInputStream ( ) , IO_BUFFER_SIZE_BYTES ) ; out = new BufferedOutputStream ( new FileOutputStream ( tempFile ) , IO_BUFFER_SIZE_BYTES ) ; int b ; while ( ( b = in . read ( ) ) != - 1 ) { out . write ( b ) ; } return tempFile ; } catch ( final IOException e ) { Log . e ( TAG , "Error in downloadBitmap - " + e ) ; } finally { if ( urlConnection != null ) { urlConnection . disconnect ( ) ; } if ( out != null ) { try { out . close ( ) ; } catch ( final IOException e ) { Log . e ( TAG , "Error in downloadBitmap - " + e ) ; } } } return null ; }
download a bitmap from a url , write it to a disk and return the file pointer . this implementation uses a simple disk cache .
