I created an app which allows the user to download audio file and store it in their SD card. The code below is actually working, but the problem is every time I hit the download button, the whole app freezes until the download is over. How can I solve this issue so that it wont freeze whenever I hit the download button? Please help me, I have been struggling for days trying to figure out te solution for this, I would really appreciate your help a lot. Thanks in advance.
try {
URL url = new URL("http://www.yourfile.mp3");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "yourfilename.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}