Here you can find the source of downloadImageToSd(String urlSource, String pathDest)
public static Boolean downloadImageToSd(String urlSource, String pathDest) throws IOException
//package com.java2s; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class Main { /******************** Method ********************/ public static Boolean downloadImageToSd(String urlSource, String pathDest) throws IOException { HttpURLConnection mHttpURLConnection = null; FileOutputStream mFileOutputStream = null; BufferedOutputStream mBufferedOutputStream = null; BufferedInputStream mBufferedInputStream = null; File imageFile = null;/*from ww w . jav a 2s . co m*/ URL url = new URL(urlSource); mHttpURLConnection = (HttpURLConnection) url.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setReadTimeout(15 * 1000); mHttpURLConnection.setDoInput(true); mHttpURLConnection.setDoOutput(true); mBufferedInputStream = new BufferedInputStream( mHttpURLConnection.getInputStream()); imageFile = new File(pathDest); mFileOutputStream = new FileOutputStream(imageFile); mBufferedOutputStream = new BufferedOutputStream(mFileOutputStream); byte[] buffer = new byte[1024]; int length; while ((length = mBufferedInputStream.read(buffer)) != -1) { mBufferedOutputStream.write(buffer, 0, length); mBufferedOutputStream.flush(); } return true; } public static Boolean downloadImageToSd(String urlSource, String pathDest, String referer) throws IOException { HttpURLConnection mHttpURLConnection = null; FileOutputStream mFileOutputStream = null; BufferedOutputStream mBufferedOutputStream = null; BufferedInputStream mBufferedInputStream = null; File imageFile = null; URL url = new URL(urlSource); mHttpURLConnection = (HttpURLConnection) url.openConnection(); mHttpURLConnection.setRequestProperty("referer", referer); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setReadTimeout(15 * 1000); mHttpURLConnection.setDoInput(true); mHttpURLConnection.setDoOutput(true); mBufferedInputStream = new BufferedInputStream( mHttpURLConnection.getInputStream()); imageFile = new File(pathDest); mFileOutputStream = new FileOutputStream(imageFile); mBufferedOutputStream = new BufferedOutputStream(mFileOutputStream); byte[] buffer = new byte[1024]; int length; while ((length = mBufferedInputStream.read(buffer)) != -1) { mBufferedOutputStream.write(buffer, 0, length); mBufferedOutputStream.flush(); } return true; } }