Здесь я пытаюсь загрузить аудио с сервера, используя AsyncTask. Загрузка успешно завершена, onPostExecute работает нормально, но onProgressUpdate не вызывается, использую ли я publishProgrees во время работы в фоновом режиме. Вот мой код
class DownloadAudioFromURL extends AsyncTask<MyTaskParams, Integer, Boolean> {
String audiopath=null;
@Override
protected Boolean doInBackground(MyTaskParams... params) {
int count;
try {
String root = path;
System.out.println("Downloading");
URL url = new URL(params[0].path);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream(root+"/AV_"+String.valueOf(params[0].tsLong)+Utility.AUDIO_FORMAT);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("audiodownloading", String.valueOf(values[0]));// this is not print data in console
}
@Override
protected void onPostExecute(Boolean isDownload) {
if (isDownload)
{
Toast.makeText(mContext, "Audio Downloaded Successfully", Toast.LENGTH_SHORT).show();
}
}
}