Download multiple files with AsyncTask

0

I have to download different audios in an internal folder on Android, I read an xml file from an url, I have the following code:

              List<Audio> canciones = new ArrayList<>();
              canciones = parserxml.parsear(result);
              List<Audio> audios_xml = writeXMLFile(canciones); //Creo un XML interno con algunos datos más.

              for (Audio url : canciones) {

                  new  GetAudioFromServer().execute(url.getUrl_audio(),url.getNombre(), String.valueOf(canciones.indexOf(url)));
              }

                listFragmentListener.cargar(audios_xml);

With this code I download all the audios I want in the correct folder but I call the GetAudioFromServer () method, as many times as there are songs in the XML from the url. The GetAudioFromServer method is as follows:

  class GetAudioFromServer extends AsyncTask<String, Integer, String>{

    HttpHandler conexion;
    File outputFile = null;
    File cancion  = null;


    @Override
    protected void onPreExecute() {
        verifyStoragePermissions(MainActivity.this);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Descargando audio..."+ url[1]+".mp3");
        pDialog.setTitle("Descargando");

        pDialog.setIndeterminate(false);
        pDialog.setMax(100);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.setCancelable(true);
        pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                cancel(true);
            }
        });

        pDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
      //  super.onProgressUpdate(progress);
        pDialog.setMessage("Descargando audio..."+ url[1]+".mp3");
        pDialog.setProgress(progress[0]);
        listFragmentListener.cambios();
    }


    @Override
    protected String doInBackground(String... url) {


            conexion = new HttpHandler();

                try {

                    File mFolder = new File(Environment.getExternalStorageDirectory(), "Directorio_interno");
                    if (!mFolder.exists()) {
                        boolean b = mFolder.mkdirs();

                    }
                    cancion = new File(String.valueOf(Environment.getExternalStorageDirectory() + "/Directorio_Interno"));
                    outputFile = new File(cancion, url[1] + ".mp3");

                    FileOutputStream fos = new FileOutputStream(outputFile);

                    InputStream is = conexion.CallServer(url[0]);
                    int fileLength = conexion.getFileLength();

                    byte[] buffer = new byte[1024];
                    int count = -1;
                    long total = 0;
                    while ((count = is.read(buffer)) != -1) {
                        total += count;
                        publishProgress((int) (total * 100 / fileLength));

                        fos.write(buffer, 0, count);

                        if (isCancelled()) {
                            outputFile.delete();
                            break;
                        }
                    }

                    fos.flush();
                    fos.close();
                    is.close();
                } catch (Exception e) {
                    System.out.println("Error" + e);
                }

            return url[2];

    }

    @Override
    protected void onPostExecute(String result) {

        if (outputFile != null){
            pDialog.setMessage("Descargando..");


            listFragmentListener.cambios();
            pDialog.dismiss();
        } else {
            pDialog.setMessage("Fallo al descargar");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    pDialog.setMessage("volviendo a descargar");
                }
            },3000);
        }

        if (result.equals("ok")){

        }
        listFragmentListener.cambios();


    }


}

The problem I have is that it launches as many ProgressDialog as there are songs in the xml, and it is logical since I call the GetAudioFromServer so many times in the loop indicated above, what would be the most efficient way to do this and that a single progressdialog indicating the total of songs and the percentage of download? Thank you very much

    
asked by F.Sola 10.05.2018 в 13:32
source

0 answers