Android : download function in separate thread

on Monday, November 3, 2014


in my android application there gonna be some files to download once in a while. so I've written a download function like this:



public static void download(Context context, String url, String path) {
URLConnection conection = new URL(url).openConnection();
conection.connect();
InputStream is = new URL(url).openStream();
OutputStream os = context.openFileOutput(path, 0);
pipe(is, os); // write from inputstream to outputstream
os.close();
}


I can't decide to put this function in a separate thread or not?

when I put it in a separate thread then if I want to downalod 10 files with a for-loop then all of them begin downloading in 10 simultaneously separate threads which is not good! when I don't put in a separate thread then main thread must wait till the downloading finishes which is not good too!


what's the solution?


0 comments:

Post a Comment