Question

I have several files uploaded to my server , now what i am trying to do is i want to download all files one by one and save it in to one folder.

So basically i was trying to do something like [this][1] but some how i am not able to achieve this so i think an alternate way to do it. So i am planning to download all audio file of particular folder of server to my SD-card folder and then i will give path of SD-card to list-view and play that audio.

So basically my question is how can i download all file from server folder to my SD-card folder.

I have below piece of code which is working fine for single file.

public class ServerFileList extends Activity {

    Uri uri;
    URL urlAudio;
    ListView mListView;
    ProgressDialog pDialog;
    private MediaPlayer mp = new MediaPlayer();
    private List<String> myList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.serverfilelist);

        mListView = (ListView) findViewById(R.id.listAudio);
        // new getAudiofromServer().execute();
        new downloadAudio().execute();

        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                playSong(urlAudio + myList.get(position));
            }
        });
    }

    public void updateProgress(int currentSize, int totalSize) {
        TextView mTextView = new TextView(ServerFileList.this);
        mTextView.setText(Long.toString((currentSize / totalSize) * 100) + "%");
    }

    private void playSong(String songPath) {
        try {
            mp.reset();
            mp.setDataSource(songPath);
            mp.prepare();
            mp.start();
        } catch (IOException e) {
            Log.v(getString(R.string.app_name), e.getMessage());
        }
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ServerFileList.this);
            pDialog.setMessage("Downloading File list from server, Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

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

            // set the download URL, a url that points to a file on the internet
            // this is the file to be downloaded
            URL url = null;
            try {
                url = new URL("http://server/folder/uploadAudio/abcd.mp3");
            } catch (MalformedURLException e3) {
                // TODO Auto-generated catch block
                e3.printStackTrace();
            }

            // create the new connection
            HttpURLConnection urlConnection = null;
            try {
                urlConnection = (HttpURLConnection) url.openConnection();
            } catch (IOException e5) {
                // TODO Auto-generated catch block
                e5.printStackTrace();
            }

            // set up some things on the connection
            try {
                urlConnection.setRequestMethod("GET");
            } catch (ProtocolException e4) {
                // TODO Auto-generated catch block
                e4.printStackTrace();
            }
            urlConnection.setDoOutput(true);

            // and connect!
            try {
                urlConnection.connect();
            } catch (IOException e3) {
                // TODO Auto-generated catch block
                e3.printStackTrace();
            }

            // set the path where we want to save the file
            // in this case, going to save it on the root directory of the
            // sd card.
            String MEDIA_PATH = new String(
                    Environment.getExternalStorageDirectory()
                            + "/newDirectory/");
            File SDCardRoot = new File(MEDIA_PATH);

            if (!SDCardRoot.exists()) {
                SDCardRoot.mkdir();
            }
            // create a new file, specifying the path, and the filename
            // which we want to save the file as.
            File file = new File(SDCardRoot, System.currentTimeMillis()
                    + ".mp3");

            // this will be used to write the downloaded data into the file we
            // created
            FileOutputStream fileOutput = null;
            try {
                fileOutput = new FileOutputStream(file);
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            // this will be used in reading the data from the internet
            InputStream inputStream = null;
            try {
                inputStream = urlConnection.getInputStream();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // this is the total size of the file
            int totalSize = urlConnection.getContentLength();
            // variable to store total downloaded bytes
            int downloadedSize = 0;

            // create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; // used to store a temporary size of the
                                    // buffer

            // now, read through the input buffer and write the contents to the
            // file
            try {
                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    // add the data in the buffer to the file in the file output
                    // stream (the file on the sd card
                    fileOutput.write(buffer, 0, bufferLength);
                    // add up the size so we know how much is downloaded
                    downloadedSize += bufferLength;
                    // this is where you would do something to report the
                    // prgress,
                    // like this maybe
                    updateProgress(downloadedSize, totalSize);

                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // close the output stream when done
            try {
                fileOutput.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;

            // catch some possible errors...

        }

        protected void onPostExecute(String file_url) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
        }
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ServerFileList.this);
            pDialog.setMessage("Getting File list from server, Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @SuppressWarnings("unchecked")
        @Override
        protected String doInBackground(String... arg0) {
            try {
                urlAudio = new URL("http://server/folder/uploadAudio");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            ApacheURLLister lister1 = new ApacheURLLister();
            try {
                myList = lister1.listAll(urlAudio);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String file_url) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    ServerFileList.this, android.R.layout.simple_list_item_1,
                    myList);
            adapter.notifyDataSetChanged();
            mListView.setAdapter(adapter);
            mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            mListView.setCacheColorHint(Color.TRANSPARENT);
        }
    }
     }
Was it helpful?

Solution 2

I have solved the problem by adding ivy jar file as a library and then added following code.

        try {
            urlAudio = new URL("http://server/folder/uploadAudio");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ApacheURLLister lister1 = new ApacheURLLister();
        try {
            myList = lister1.listAll(urlAudio);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

OTHER TIPS

Try follow steps,

  1. Write a server side component which will return back the list of the files in the directory in which all the audio files are hosted.

  2. In your downloadAudio class, instead of downloading a file, initially make a call to the component described above to get the list.

  3. Move the code which you have written in an function which will take String argument.

  4. Loop on the url list from step 2 and call function in step 3 with parameter as current element of url list.

  5. Keep all checks and balances to ensure all the files are downloaded.

Your issue is more of design issue then programming.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top