source credit: http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
package nabar.library.filemanager;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.os.StrictMode;
import android.util.Log;
public class FileManager{
private final String filePath = "/sdcard/nabar/";
private final String urlPath = "http://download url";
public void downloadFromUrl(String fileURL, String fileName){
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
File fileDirectory = new File(filePath);
if(!fileDirectory.exists()){
if(!fileDirectory.mkdirs()){
Log.e("FileManager::", "Problem creating Nabar directory");
}
}
try {
URL url = new URL(urlPath+fileURL); //you can write here any link
File file = new File(filePath+fileName);
long startTime = System.currentTimeMillis();
Log.d("FileManager", "download begining");
Log.d("FileManager", "download url:" + url);
Log.d("FileManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("FileManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("FileManager", "Error: " + e);
}
}
}