public static byte[] downloadBitmapToMemory(Context context,String urlString,int maxBytes){
  HttpURLConnection urlConnection=null;
  ByteArrayOutputStream out=null;
  InputStream in=null;
  try {
    final URL url=new URL(urlString);
    urlConnection=(HttpURLConnection)url.openConnection();
    if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
      return null;
    }
    in=new BufferedInputStream(urlConnection.getInputStream(),IO_BUFFER_SIZE_BYTES);
    out=new ByteArrayOutputStream(IO_BUFFER_SIZE_BYTES);
    final byte[] buffer=new byte[128];
    int total=0;
    int bytesRead;
    while ((bytesRead=in.read(buffer)) != -1) {
      total+=bytesRead;
      if (total > maxBytes) {
        return null;
      }
      out.write(buffer,0,bytesRead);
    }
    return out.toByteArray();
  }
 catch (  final IOException e) {
    Log.e(TAG,"Error in downloadBitmapToMemory - " + e);
  }
 finally {
    if (urlConnection != null) {
      urlConnection.disconnect();
    }
    try {
      if (out != null) {
        out.close();
      }
      if (in != null) {
        in.close();
      }
    }
 catch (    final IOException e) {
      Log.e(TAG,"Error in downloadBitmapToMemory - " + e);
    }
  }
  return null;
}

public static byte [ ] downloadBitmapToMemory ( Context context , String urlString , int maxBytes ) { HttpURLConnection urlConnection = null ; ByteArrayOutputStream out = null ; InputStream in = null ; try { final URL url = new URL ( urlString ) ; urlConnection = ( HttpURLConnection ) url . openConnection ( ) ; if ( urlConnection . getResponseCode ( ) != HttpURLConnection . HTTP_OK ) { return null ; } in = new BufferedInputStream ( urlConnection . getInputStream ( ) , IO_BUFFER_SIZE_BYTES ) ; out = new ByteArrayOutputStream ( IO_BUFFER_SIZE_BYTES ) ; final byte [ ] buffer = new byte [ 128 ] ; int total = 0 ; int bytesRead ; while ( ( bytesRead = in . read ( buffer ) ) != - 1 ) { total += bytesRead ; if ( total > maxBytes ) { return null ; } out . write ( buffer , 0 , bytesRead ) ; } return out . toByteArray ( ) ; } catch ( final IOException e ) { Log . e ( TAG , "Error in downloadBitmapToMemory - " + e ) ; } finally { if ( urlConnection != null ) { urlConnection . disconnect ( ) ; } try { if ( out != null ) { out . close ( ) ; } if ( in != null ) { in . close ( ) ; } } catch ( final IOException e ) { Log . e ( TAG , "Error in downloadBitmapToMemory - " + 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 .
