I need function that download files by passing name and urls, so that i can download series of files in android app.
So went ahead and created something like this:
public void doDownload (String fileName, String url){
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/Content/");
if(dir.exists()==false) {
dir.mkdirs();
}
try{
URL url = new URL(url);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
Log.i("ANDRO_ASYNC", "Lenght of file: " + fileLength);
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(dir + fileName);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch(Exception e){
}
}
XML: Added permission
Now when i call this function:
doDownload(String img.png, String http://server.com);
It returns no files. What is wrong here?