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

2

5

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

Reubenreuchlin answered 10/6, 2021 at 10:55 Comment(1)
Of course you can. Better look at request.setDestination... functions.Nuristan
R
3

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");
Reubenreuchlin answered 11/6, 2021 at 11:15 Comment(2)
Thank you so much for your answer. I just wanted to know whether the downloaded file exists or not and your code helpedCapone
can I know how can I share the video using this method?Capone
C
4

Check the request.setDestination functions here To Store file in External App-Specific Directory [example: "external/Android/data/your_app_name/filePath_you_set_in_function”], use like below:

DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Selected Video is being downloaded");
request.allowScanningByMediaScanner();
request.setTitle("Downloading Video");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName); //To Store file in External Public Directory use "setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)"
DownloadManager manager = (DownloadManager)
mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

And if you want to download it to another place you need to move it after your download gets finished, by using IO streams. You can use a broadcast receiver to perform this task once the DownloadManager has finished downloading. You can use FileProvider to open the file with another app in Android version 10 or above.

Cullender answered 10/6, 2021 at 12:2 Comment(5)
"Path" is not a public dir so that will not go. And they are no callbacks.Nuristan
Sorry, but your update makes it even worse by supplying a full parh. And the legacy permission request is not needed. Never.Nuristan
@blackapps, This working well at my end. Please share the other option if you have one.Cullender
@Cullender but how to retrieve and delete that specific file. (I am using request.setDestinationInExternalFilesDir)Reubenreuchlin
@SachinBurdak, You have already answered!. yes, to get the specific file use "context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName)"Cullender
R
3

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");
Reubenreuchlin answered 11/6, 2021 at 11:15 Comment(2)
Thank you so much for your answer. I just wanted to know whether the downloaded file exists or not and your code helpedCapone
can I know how can I share the video using this method?Capone

© 2022 - 2024 — McMap. All rights reserved.