Reputation: 207
I have a problem i get -1 as length of the file. I tried a lot of time to fix it. I don't know why this happens. Also when i call again the asynctask , song1 stays the same(but the song1 value have not any problem, i tested with a toast and it works ). the 'song1' is a string and each time have different values. Thank you.
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading "+song1+"..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
((HttpURLConnection) conexion).setInstanceFollowRedirects(true);
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/download/"+song1);
byte data[] = new byte[20480];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total/lenghtOfFile)*100));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
and here is my LogCat
03-08 20:25:17.714: D/ANDRO_ASYNC(14559): Lenght of file: -1
03-08 20:25:18.564: D/dalvikvm(14559): GC freed 18632 objects / 1179128 bytes in 112ms
03-08 20:25:18.864: I/global(14559): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
03-08 20:25:18.874: D/ANDRO_ASYNC(14559): -420000
03-08 20:25:18.954: D/ANDRO_ASYNC(14559): -700000
03-08 20:25:18.984: D/ANDRO_ASYNC(14559): -1820000
03-08 20:25:19.029: D/ANDRO_ASYNC(14559): -1960000
Upvotes: 2
Views: 826
Reputation: 19
web.setDownloadListener { url, userAgent, contentDisposition, mimeType, contentLength ->
val ul = url
DownloadFile(this, url,contentLength).execute(ul)
}
Upvotes: 1
Reputation: 77627
Getting a length of -1 isn't uncommon. Usually that means that the server is sending it's buffer of data to you (likely because it is full), but it still does not know the entire size of the response. Or it's a lazy developer.
With a length of -1, you just need to keep loading information until you are at the end of the stream.
Upvotes: 0