0

I have Application play some mp3 files from the internet and I want to add "Download Button" to download any mp3 file from the internet I want to download from the browser ,,, not from Application ,,, but if I want to call the browser such as :


startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("xxxx.xxxx.xxxx.mp3")));


the browser going to play this mp3 file :(

I think I can add thing after the link to download directly ,,,, any one can help me ?

Note : if you have a code to download from the App ,,, tell me about it also :) I'm new in android world :P and I want a simple code to can understand it thank you all :)

Oubaida AlQuraan
  • 1,706
  • 1
  • 18
  • 19

2 Answers2

5

You would be better off using the DownloadManager. You can see example code by Lars Vogel here

The relevant bit:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png"));
        enqueue = dm.enqueue(request);

Note that DownloadManager is available on API version 9 (Android 2.3, Gingerbread) and upwards.

WeNeigh
  • 3,489
  • 3
  • 27
  • 40
  • sure , the better is DownloadManager :) but I want to ask you how I can determination the path on SD too ?! – Oubaida AlQuraan Jan 25 '13 at 12:09
  • Use the setDestinationInExternalPublicDir method of the Request object. For example, to save the mp3 file as "song.mp3" in the default MUSIC directory, use request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "song.mp3"); – WeNeigh Jan 25 '13 at 12:22
  • Oops !! Forgive me, I forget a small thing ,,, when the download end ,,, how I can open the file path ? – Oubaida AlQuraan Jan 25 '13 at 12:36
  • File musicFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "song.mp3"); – WeNeigh Jan 25 '13 at 12:37
  • You're give me the path only ,,, I want when the dm end download open this path directly ,,, :D and you are forgive the : :P – Oubaida AlQuraan Jan 25 '13 at 12:54
0

You can put this in your download.php file

 <?php
     $file = $_GET['file'];
     header ("Content-type: octet/stream");
     header ("Content-disposition: attachment; filename=".$file.";");
     header("Content-Length: ".filesize($file));
     readfile($file);
     exit;
   ?>

and then:

 Uri.parse("direct_download.php?file=filename.mp3");
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
  • vodich I wanna download an mp4 file from youtube like http://r4---sn-u2oxu-f5f6.googlevideo.com/videoplayback?.. blah blah.mp4. Tell me if i call it where it will be downloaded and how much code of php i have to change. – Zar E Ahmer Jul 30 '15 at 11:27