Android
Android

Reputation: 9033

need help for android mp3 file download

i am developing an android application into it i need to download mp3 file

but the download process should be into the background.
does any one have any idea about this?

Upvotes: 2

Views: 2131

Answers (2)

Android
Android

Reputation: 9033

Finally I've got the solution.

public void mp3load() {
        URL url = new URL(url);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        String PATH = Environment.getExternalStorageDirectory()
                + "/download/";
        Log.v(LOG_TAG, "PATH: " + PATH);
        File file = new File(PATH);
        file.mkdirs();

         String fileName = "test.mp3";


        File outputFile = new File(file, fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);

        InputStream is = c.getInputStream();

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();

}

It works good for me.

Upvotes: 4

J.S. Taylor
J.S. Taylor

Reputation: 761

android: Is it possible to dowload file to disk given an xml (from a URL)?

Check this out: a AsyncTask class with a downloadFileFrom method will help you out.

Upvotes: 0

Related Questions