Download Remote File Android

[ITA] Ecco uno snippet di codice utile per poter scaricare un file localizzato su un server remoto
[ENG] This is a code snippet useful to download a file


public String downloadURL (URL url, String fileName) throws IOException {
String destination = null;

try {
destination = Environment.getExternalStorageDirectory() + "/"
+ "myapp/" + fileName;

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destination);

byte buffer[] = new byte[1024];
int bytes = 0;

while ((bytes = input.read(buffer)) != -1) {
output.write(buffer,0, bytes);
}

output.flush();
output.close();
input.close();

}
catch (Exception e) {
Log.e("DOWNLOAD URL", "downloading file failed due to " + e.toString());
}

return destination;
}

Commenti

commenti

Lascia un commento