public void DownloadFromUrl(String imageURL, String fileName) {
//this is the downloader method
try {
URL url = new URL("http://picosong.com/wvaV");
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "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("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
}
Asked
Active
Viewed 2.2k times
3

Akshay
- 2,506
- 4
- 34
- 55

user1761098
- 57
- 1
- 2
- 3
-
@Siddharth no idea ♂️ – Pavlos Aug 01 '18 at 10:25
-
may be add some text in the question.. – Siddharth Aug 02 '18 at 03:22
2 Answers
8
Try below code for download file.
private void startDownload() {
String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
new DownloadFileAsync().execute(url);
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
And see below link for more information.

Dipak Keshariya
- 22,193
- 18
- 76
- 128
-
The url which i have given is of a website...i want things that i go on that url page and hit download button and that downloaded file must be saved in the path which i have given. – user1761098 Oct 30 '12 at 07:43
-
This answer work perfect. Thanks a lot. This should be the correct answer. – Dinith Rukshan Kumara May 26 '18 at 10:10
3
Try this code
try{
File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Folder Name");
if(!cacheDir.exists())
cacheDir.mkdirs();
File f=new File(cacheDir,songname+".mp3");
URL url = new URL(yoururl);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(f);
byte data[] = new byte[1024];
long total = 0;
int count=0;
while ((count = input.read(data)) != -1) {
total++;
Log.e("while","A"+total);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
Catch(Exception e){e.printStackTrace();}

maninder singh
- 1,286
- 13
- 16
-
The url which i have given is of a website...i want things that i go on that url page and hit download button and that downloaded file must be saved in the path which i have given. – user1761098 Oct 30 '12 at 07:42