Lisa Anne
Lisa Anne

Reputation: 4595

I write a file to Environment.DIRECTORY_DOWNLOADS, why can't I see it via the Downloads App?

I write (write, not download, to be precise it is the dump of a SQLite db of my App) a file on the Environment.DIRECTORY_DOWNLOADS directory.

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "db.csv");

If I browse the phone with a file browser, I can see correctly the file in the

  /storage/emulated/0/Download

Directory, together with the other downloads.

But if I open the Downloads App it does not show...

What do I need to do to have the file shown in the Downloads App as well?

Upvotes: 18

Views: 28614

Answers (6)

CoolMind
CoolMind

Reputation: 28793

If you use setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName), look for a file in /sdcard/Android/data/<package name>/files/Download. In emulator it can be found in Device File Explorer.

enter image description here

If you use setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName), then you can find a downloaded file in Download folder of Files application.

enter image description here

Upvotes: 1

user1673554
user1673554

Reputation: 461

Try setting the setVisibleInDownloadsUi property.
This allows the download to be visible in the Downloads app.
See Download.Manager

UPD: This method was deprecated in API level 29.

Upvotes: 2

rontho
rontho

Reputation: 439

In case anyone wonder how to do this, I found the answer here

To allow the System to display you file in the "Downloads" App, you need to explicitly notify the DownloadManager that a download as been completed by using the method addCompletedDownload()

By using it, the system will allow your Download App to display the file you have created yourself.

Hope it will help.

UPD: This method was deprecated in API level 29.

Upvotes: 9

jpussacq
jpussacq

Reputation: 577

Add your file to downloaded list:

DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
dm.addCompletedDownload("db.csv", "my description", false, "text/csv", file.getAbsolutePath(), file.length(), false);

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006644

The Downloads app only shows downloads downloaded via DownloadManager, not all files in the directory.

Upvotes: 8

Gopal Gopi
Gopal Gopi

Reputation: 11131

you just created File instance like

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "db.csv");

but the file "db.csv" is not created yet. use

file.createNewFile();

to create the file "db.csv" really...

Upvotes: 2

Related Questions