Проблема с загрузкой файлов из сети

Рейтинг: 1Ответов: 1Опубликовано: 28.03.2011
public void StartDownloading()
{
    File file = null;

    for(int i=0; i<FileNames.length; i++) 
    {
         file = new File("/sdcard/Sounds/"+FileNames[i]);
         boolean exists = file.exists();
         if(exists)
         {
             continue;
         }
         else 
         {
             new DownloadFileAsync().execute(Path+FileNames[i],FileNames[i]);               
         }
         file = null;
     }               
}

@Override
protected Dialog onCreateDialog(int id) 
{
    switch (id) 
    {
        case DIALOG_DOWNLOAD_PROGRESS:
            WaitDialog = new ProgressDialog(this);
            WaitDialog.setMessage("Downloading files...");
            WaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            WaitDialog.setCancelable(false);
            WaitDialog.show();
            return WaitDialog;
        default:
            return null;
    }
}

class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;

        try 
        {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream());
            File file = new File("/sdcard/Sounds/"+aurl[1]);
            OutputStream output = new FileOutputStream(file);
            byte data[] = new byte[4046848];

            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) {}

        return null;
    }

    @Override
    protected void onPostExecute(String unused) 
    {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);         
    }
}

При этом я пытаюсь загрузить 4 файла суммарным весом 2.71 мб, прогресс бар появляется, но обычно быстро пропадает, и файлы остаются загруженные по 5-30кб, иногда даже появляются 1-2 файла тоже по 5-30кб.

Ответы

Ответов пока нет.