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