In an android app, I am trying to download a file from a web server to /Download folder on external storage. download code is executed in a HandlerThread
in a service.
The service is doing other functions apart from downloading file. the code for downloading goes like this:
public void downloadFile(){
new Thread(new Runnable() {
@Override
public void run() {
try{
URL url = new URL("http://192.168.1.105/download/apkFile.apk");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream inputStream = connection.getInputStream();
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/Download/apkFile.apk");
FileOutputStream fileOutputStream = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[4096];
while((bytesRead = inputStream.read(buffer)) != -1){
fileOutputStream.write( buffer, 0, bytesRead);
}
fileOutputStream.close();
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
There is no error in executing but the file is not downloaded. Please suggest.