0

I have developed an mp3 downloader which downloads mp3 from a URL to android phone using DownloadManager. But the problem is that if you uninstall the app, the files previously downloaded will get removed from the folder. I would like to get alternate methods or libraries to download mp3 files (only mp3) from a URL to android device.

storage = Environment.DIRECTORY_MUSIC;

request = new DownloadManager.Request(Uri.parse(downloadUrl));
                                extension =".mp3";
                                request.setMimeType("audio/MP3");
                                request.setTitle(vMeta.getTitle());
                                request.allowScanningByMediaScanner();
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                request.setDestinationInExternalPublicDir(storage, vMeta.getTitle() + extension);
                                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                manager.enqueue(request);

vMeta.getTitle() - returns the title of the mp3 file and ext contains ".mp3" .

The file is getting downloaded and it is working fine.

NOTE : I have also tried DownloadManager.Request

PSN
  • 2,326
  • 3
  • 27
  • 52
  • Can I use it like this ? setDestinationUri(storage, vMeta.getTitle() + extension); – PSN Mar 17 '18 at 08:01
  • request.setDestinationInExternalFilesDir(getActivity(), Environment.DIRECTORY_DOWNLOADS,"put whatever you want"); – iamkdblue Mar 17 '18 at 08:02
  • setDestinationUri(Uri.parse(storage + vMeta.getTitle() + extension)) ; – PSN Mar 17 '18 at 08:03
  • `storage + vMeta.getTitle() + extension` ??? Nobody knows what path that would be. It makes no sense to post such code. – greenapps Mar 17 '18 at 08:04
  • Try **AsyncTask** as describe [here](https://stackoverflow.com/a/3028660/7756492). – ucMedia Mar 17 '18 at 08:06
  • @MikeM. I tried this : ` request.setDestinationUri(Uri.fromFile(new File(Environment.DIRECTORY_MUSIC, vMeta.getTitle() + extension))); ` and I got this error : java.lang.SecurityException: Destination must be on external storage: file:///Music/Pt%20.Kumar%20Bose%20and%20anindo%20chattergee%20in%20green%20room%20practice%20-1.mp3 – PSN Mar 17 '18 at 08:25
  • @MikeM. Yes it is still happening. Confirmed in different mobile phones running Android 6.0 – PSN Mar 17 '18 at 08:42
  • Confirmed. It also happens with my suggested method, too. That's unexpected. – Mike M. Mar 17 '18 at 08:54
  • Aha! Found a pretty simple workaround, though. If you rename the file after it's downloaded, it doesn't get deleted upon uninstallation. You could download it to a temp name, and just rename it when it's done. – Mike M. Mar 17 '18 at 09:08
  • Yep, even works in the public storage directories. So you could, for example, download it with a .tmp extension, then rename it with an .mp3 extension after the download. – Mike M. Mar 17 '18 at 09:16
  • @MikeM. Could you post the code as the answer? – PSN Mar 17 '18 at 09:16
  • Sure. Gimme a little bit to put something together. – Mike M. Mar 17 '18 at 09:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167014/discussion-between-psn-and-mike-m). – PSN Mar 17 '18 at 09:37

0 Answers0