Android : download content from URL which returns mp3 file
<p>After 2 days of research, I didn't found any solution to my problem :/</p>

<p>I want to download an mp3 file, which is returned only after calling a URL in a browser.</p>

<p>I can't give you the actual url because I'm not allowed to due to rights restrictions, but it is of the form:</p>

<pre><code>http://wscompany.name.com/downloadws/getDlFile/mdkHdKy97RppVWOsIOdDBuG/audio/1478
</code></pre>

<p>As you can see, this URL doesn't have the mp3 extension. So if I put this kind of URL in a browser on windows, it returns an MP3 to save onto the disk, which is fine. But if I want to call this URL in android in order to download the final file returned (the mp3), it doesn't work.</p>

<p>I tried with a url containing a mp3 file directly and it works very well (like <a href="http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3" rel="nofollow">http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3</a>), but not with an url without the mp3 extension, even though it does returns an mp3, I hope you see what I mean.</p>

<p>Does anyone know how to do that in android ?</p>

<p>Here is my code, using an AsyncTask, called by</p>

<pre><code>new Download(MyActivity.this, urlToCall).execute();
</code></pre>

<p>And the Download AsyncTask :</p>

<pre><code>public class Download extends AsyncTask&lt;String, Void, String&gt; 
{
    ProgressDialog mProgressDialog;

    Context context;
    String urlDownload;

    public Download(Context context,String url) 
    {
        this.context = context;
        this.urlDownload=url;
    }

    protected void onPreExecute() 
    {
        mProgressDialog = ProgressDialog.show(context, "","Please wait, Download for " + urlDownload );
        Log.v("DOWNLOAD", "Wait for downloading url : " + urlDownload);
    }

    protected String doInBackground(String... params) 
    {
        try 
        {
            //URL url = new URL("http://www.mediacollege.com/downloads/sound-effects/urban/factory/Factory_External_01.mp3");
            URL url = new URL(urlDownload);

            Log.w( "DOWNLOAD" , "URL TO CALL : " + url.toString());
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            File folder = new File(Environment.getExternalStorageDirectory()+ "/MyDownloaded/") ;

            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }

            File file = new File(folder,"somefile.mp3");

            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();

            //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
            while ( (bufferLength = inputStream.read(buffer)) &gt; 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);
                Log.w( "DOWNLOAD" , "progress " + downloadedSize + " / " + totalSize);

            }
            //close the output stream when done
            fileOutput.close();

        //catch some possible errors...
        } 
        catch (MalformedURLException e) 
        {
            Log.e( "DOWNLOAD" , "ERROR : " + e );
        } 
        catch (IOException e) 
        {
            Log.e( "DOWNLOAD" , "ERROR : " + e );
        }
        return "done";
    }

    private void publishProgress( int i )
    {
        Log.v("DOWNLOAD", "PROGRESS ... " + i);
    }

    protected void onPostExecute(String result) 
    {
        if (result.equals("done")) 
            mProgressDialog.dismiss();
    }
</code></pre>

<p>Thank you in advance, I hope someone will help me :)</p>

<p>DevJ</p>
