Tôi đang tạo ứng dụng Android và tôi cần tải xuống tệp từ url, lớn 33 MB.Android: "Ngoại lệ không mong muốn" ngoại lệ tải xuống các tệp lớn
Đây là nhiệm vụ download:
try {
int MAX_BUFFER_SIZE = 4096;
URL mUrl = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
connection.setRequestMethod("GET");
long length = connection.getContentLength(), downloaded = 0;
int read;
byte [] buffer = new byte[(((int)length) > MAX_BUFFER_SIZE) ? MAX_BUFFER_SIZE : (int)length];
String filename = getFilename(mUrl);
File file = new File (SDCARD_ROOT);
if (!file.exists() || !file.isDirectory()){
file.mkdir();
}
this.filename = filename;
file = new File (SDCARD_ROOT + this.filename);
FileOutputStream fos = new FileOutputStream (file);
//Start downloading
InputStream stream = connection.getInputStream();
while ((read=stream.read(buffer)) > -1){
fos.write(buffer, 0, read);
downloaded += read;
publishProgress((int) ((float) downloaded/length * 100));
}
fos.close();
return 1;
} catch (Exception e){
Log.e("REV-PARTS", "Revolver parts error in DownloadTask: " + e.getMessage());
return 2;
}
Nó hoạt động đúng với các file nhỏ (1-15 mb), nhưng nó sẽ trả về một "kết thúc bất ngờ của dòng" ngoại lệ với các tập tin lớn. Cảm ơn trước.
Cuộc gọi nào ném ngoại lệ? 'stream.read()'? –
Yep, stream.read – Gnufabio
@Gnufabio bạn có thể khắc phục sự cố này không? –