Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the downloaded file path using the download manager

Tags:

file

android

path

I am able to download a video from the server using the download manager. However when I log the path using below code.

 String path = Environment.getExternalStoragePublicDirectory(directory).getAbsolutePath() + subpath;
 Log.e("PATH", path);

I get

12-15 13:29:36.787 22807-22807/com.ezyagric.extension.android E/PATH: /storage/sdcard0/EZYAGRIC/Soil Testing.mp4.

Now this is different from the path on the phone which is

/storage/sdcard0/Android/data/com.ezyagric.extension.android/files/EZYAGRIC/Crop Insurance.mp4

What brings that difference and how can obtain the path in the phone the way it is?

like image 700
Raphael Avatar asked Dec 10 '22 08:12

Raphael


2 Answers

Code snippet to download file in default download directory.

DownloadManager.Request dmr = new DownloadManager.Request(Uri.parse(url));

// If you know file name
String fileName = "filename.xyz"; 

//Alternative if you don't know filename
String fileName = URLUtil.guessFileName(url, null,MimeTypeMap.getFileExtensionFromUrl(url));

dmr.setTitle(fileName);
dmr.setDescription("Some descrition about file"); //optional
dmr.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
dmr.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
dmr.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(dmr);

Note For mContext.getSystemService

  • Activity= getSystemService();
  • Fragment= getActivity.getSystemService();
  • Adapter= mContext.getSystemService(); //pass context in adapter

UPDATE

As OP want to check file exist or not

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName); 
if(file.exists()){//File Exists};
like image 94
Abhishek Singh Avatar answered May 12 '23 04:05

Abhishek Singh


You should try to download in download folder

        String url = "url you want to download";
        DownloadManager.Request request = new 
        DownloadManager.Request(Uri.parse(url));
        request.setDescription("Some descrition");
        request.setTitle("Some title");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

        // get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
like image 22
Nirav Joshi Avatar answered May 12 '23 04:05

Nirav Joshi