У меня есть следующий код для скачивания файла с url на sdcard. Этот код отлично работает для небольшого файла, но когда размер файла большой, я получаю загруженный размер файла 0. Любая помощь будет оценена.Скачивание файла в sdcard с указанием размера 0
Java Code
setContentView(R.layout.activity_download_file);
String exStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(exStorageDirectory, "Folder");
folder.mkdir();
File file = new File(folder, "scjp.pdf");
try {
if (!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
downloadFile(
"http://java.net/downloads/jfjug/SCJP%20Sun%20Certified%20Programmer%20for%20Java%206-0071591060.pdf",
file);
}
private void downloadFile(String fileUrl, File directory) {
try {
FileOutputStream fileOutput = new FileOutputStream(directory);
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
byte buffer[] = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, len);
}
fileOutput.close();
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Я думаю, вы должны поместить загрузку в AsyncTask или Thread – Gyonder
+1 для SCJP Book – Jacob
@Gyonder может у вас рассказать мне wathz о проблеме здесь почему для большого файла он показывает файл, загруженный 0? – sonia