Sto costruendo un'applicazione Android e ho bisogno di scaricare un file da un url, che è grande 33 MB.Android: eccezione "Inaspettata fine del flusso" di download di file di grandi dimensioni
Qui l'attività di 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;
}
Funziona scorretto con file di piccole dimensioni (1-15 mb), ma restituirà un "fine inaspettata di flusso" eccezione con file di grandi dimensioni. Grazie in anticipo.
Quale chiamata genera l'eccezione? 'Stream.Read()'? –
Sì, stream.read – Gnufabio
@Gnufabio, sei riuscito a risolvere questo problema? –