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

前端 未结 5 971
余生分开走
余生分开走 2020-12-03 11:45

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 = Env         


        
相关标签:
5条回答
  • 2020-12-03 12:00

    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);
    
    0 讨论(0)
  • 2020-12-03 12:06

    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...

    0 讨论(0)
  • 2020-12-03 12:10

    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.

    0 讨论(0)
  • 2020-12-03 12:17

    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.

    0 讨论(0)
  • 2020-12-03 12:20

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

    0 讨论(0)
提交回复
热议问题