src/net/stkaddons/viewer/Network.java
public static File downloadFile(Context context, String urlString, String destFilename) throws IOException {
        InputStream stream = null;
        FileOutputStream output = null;
        HttpURLConnection conn = null;
        
        try {
            URL url = new URL(urlString);
            File cacheFile = new File(context.getCacheDir(), destFilename);
            
        	// Check for existing copy of the file
            long maxCacheAge = 2*24*3600*1000;
            if (cacheFile.exists() && (System.currentTimeMillis() - cacheFile.lastModified()) < maxCacheAge) {
            	Log.i("downloadFile", "Using cached file: " + cacheFile.getPath());
            	return cacheFile;
            }

            if (!isConnected(context))
            	return null;
            
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", Network.mUserAgent);
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            stream = new BufferedInputStream(conn.getInputStream());
            
            // Write cache file
            cacheFile.createNewFile();
            output = new FileOutputStream(cacheFile);
            int bufferSize = 1024;
	        byte[] buffer = new byte[bufferSize];
	        int len = 0;
	        while ((len = stream.read(buffer)) != -1) {
	            output.write(buffer, 0, len);
	        }
	        
	        return cacheFile;
            
        // Makes sure the streams and connections are closed
        } finally {
            if (stream != null) {
                stream.close();
            }
            if (output != null) {
            	output.close();
            }
            if (conn != null) {
            	conn.disconnect();
            }
        }
    }
/**
     * Download a file to cache directory
     * @param context
     * @param urlString
     * @return Reference to cache file
     * @throws IOException
     */
Download a file to cache directory
@param context
@param urlString
@return Reference to cache file
@throws IOException
Download a file to cache directory @param context @param urlString @return Reference to cache file @throws IOException
public static File downloadFile(Context context, String urlString, String destFilename) throws IOException {
        InputStream stream = null;
        FileOutputStream output = null;
        HttpURLConnection conn = null;
        try {
            URL url = new URL(urlString);
            File cacheFile = new File(context.getCacheDir(), destFilename);
            long maxCacheAge = 2*24*3600*1000;
            if (cacheFile.exists() && (System.currentTimeMillis() - cacheFile.lastModified()) < maxCacheAge) {
            	Log.i("downloadFile", "Using cached file: " + cacheFile.getPath());
            	return cacheFile;
            }
            if (!isConnected(context))
            	return null;
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", Network.mUserAgent);
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            stream = new BufferedInputStream(conn.getInputStream());
            cacheFile.createNewFile();
            output = new FileOutputStream(cacheFile);
            int bufferSize = 1024;
	        byte[] buffer = new byte[bufferSize];
	        int len = 0;
	        while ((len = stream.read(buffer)) != -1) {
	            output.write(buffer, 0, len);
	        }
	        return cacheFile;
        } finally {
            if (stream != null) {
                stream.close();
            }
            if (output != null) {
            	output.close();
            }
            if (conn != null) {
            	conn.disconnect();
            }
        }
    }
public static File downloadFile ( Context context , String urlString , String destFilename ) throws IOException { InputStream stream = null ; FileOutputStream output = null ; HttpURLConnection conn = null ; try { URL url = new URL ( urlString ) ; File cacheFile = new File ( context . getCacheDir ( ) , destFilename ) ; long maxCacheAge = 2 * 24 * 3600 * 1000 ; if ( cacheFile . exists ( ) && ( System . currentTimeMillis ( ) - cacheFile . lastModified ( ) ) < maxCacheAge ) { Log . i ( "downloadFile" , "Using cached file: " + cacheFile . getPath ( ) ) ; return cacheFile ; } if ( ! isConnected ( context ) ) return null ; conn = ( HttpURLConnection ) url . openConnection ( ) ; conn . setRequestProperty ( "User-Agent" , Network . mUserAgent ) ; conn . setReadTimeout ( 10000 /* milliseconds */ ) ; conn . setConnectTimeout ( 15000 /* milliseconds */ ) ; conn . setRequestMethod ( "GET" ) ; conn . setDoInput ( true ) ; conn . connect ( ) ; stream = new BufferedInputStream ( conn . getInputStream ( ) ) ; cacheFile . createNewFile ( ) ; output = new FileOutputStream ( cacheFile ) ; int bufferSize = 1024 ; byte [ ] buffer = new byte [ bufferSize ] ; int len = 0 ; while ( ( len = stream . read ( buffer ) ) != - 1 ) { output . write ( buffer , 0 , len ) ; } return cacheFile ; } finally { if ( stream != null ) { stream . close ( ) ; } if ( output != null ) { output . close ( ) ; } if ( conn != null ) { conn . disconnect ( ) ; } } }
Download a file to cache directory
 @param context
 @param urlString
 @return Reference to cache file
 @throws IOException
Download a file to cache directory @param context @param urlString @return Reference to cache file @throws IOException
// Check for existing copy of the file // Starts the query // Write cache file // Makes sure the streams and connections are closed