Search code examples
androidandroid-download-managerandroid-storageandroid-internal-storage

how to download and save media files in internal storage android java


I want to download some audio files from the internet. I want to use downloadmanager to download this file but I don't know how to save these files to a specific internal storage location in android. InternalStorage/folder/filename.mp3 (so I can access them easily).

manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                Uri uri = Uri.parse("URL");
                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
                long reference = manager.enqueue(request);

It is useful for downloading. But we cannot specify the location for these files.

So how to specify these files to be saved in the internal storage at a specific location. And also, how to access and delete these files.

Please don't devote this question as I got confused while going through many articles.

if any other better alternative or any suggestion comment down


Solution

  • simply add this line to save file:

    request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_ALARM,mp3name);  
    

    to delete this file

    File file = new 
    File(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");
    
    file.delete();
    

    to read this file

    File file= newFile(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");