Raphael
Raphael

Reputation: 291

How to get the downloaded file path using the download manager

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?

Upvotes: 6

Views: 29068

Answers (4)

NickUnuchek
NickUnuchek

Reputation: 12857

To download file without callback you can just enqueue it.

val fileUrl = "https://example.com/myfile.pdf"
val fileName = "myfile.pdf"
val fileExtension = File(fileName).extension
 val request = DownloadManager.Request(fileUrl.toUri()).apply {
            setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS,
                fileName
            )
            setMimeType(
                MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(fileExtension)
            )
            allowScanningByMediaScanner()
            setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION)
            setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
        }

        val idDownloded = downloadManager?.enqueue(request) ?: return

But if you make the same downloads many times, then downloaded files will be stored as : "myfile-1.pdf", "myfile-2.pdf", "myfile-3.pdf"...

So, to get downloaded file path you need to do this:

val fileDownloadedUri = downloadManager.getUriForDownloadedFile(idDownloded)//content://downloads/all_downloads/43
val mimeType = downloadManager.getMimeTypeForDownloadedFile(idDownloded)

val fileDownloadedPath = FilesUtils.getPath(
            context = activity,
            uri = fileDownloadedUri
        )

where FilesUtils (https://github.com/coltoscosmin/FileUtils/blob/master/FileUtils.java) from here (https://stackoverflow.com/a/53021624/2425851)

Then you may share the downloaded file:

val file = File(fileDownloadedPath)
        if (!file.exists()) {
            //error
            return
        }

        val uriForFile = FileProvider.getUriForFile(
            activity,
            BuildConfig.APPLICATION_ID,
            file
        )

        val intent = Intent().apply {
            action = Intent.ACTION_SEND
            type = mimeType
            putExtra(Intent.EXTRA_STREAM, uriForFile)
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }

        activity.startActivity(
            Intent.createChooser(
                intent,
                "Share"
            )
        )

How to configure FileProvider see this (https://stackoverflow.com/a/51579654/2425851)

Upvotes: 0

Bhautik Domadiya
Bhautik Domadiya

Reputation: 100

When you call --> downloadManager.enqueue(request) method before set their path

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"Google.gif");

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"google.gif"); // Set Your File Name
if (file.exists()) {
    Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    pro28_imageView.setImageBitmap(myBitmap);
}

Upvotes: 2

Abhishek Singh
Abhishek Singh

Reputation: 9188

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};

Upvotes: 11

Nirav Joshi
Nirav Joshi

Reputation: 1723

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);

Upvotes: 3

Related Questions