Android アプリで、Web サーバーから外部ストレージの /Download フォルダーにファイルをダウンロードしようとしています。ダウンロードコードはHandlerThread
サービス内で実行されます。
このサービスは、ファイルのダウンロード以外の機能を実行しています。ダウンロードのコードは次のようになります。
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();
}
実行にエラーはありませんが、ファイルはダウンロードされません。提案してください。