简体   繁体   English

在android中找不到下载管理器下载的文件

[英]Can't find file downloaded by download manager in android

I'm trying to download a file to directory that can vary based on whichever directory the user chooses. 我正在尝试将文件下载到目录,该目录可能因用户选择的目录而异。 I store the current directory in a file object and now attempting to download the file. 我将当前目录存储在文件对象中,现在尝试下载该文件。 The file downloads, but it doesn't download the specified directory. 文件下载,但不下载指定的目录。 So, what can I do to get the file in the directory selected. 那么,我该怎么做才能将文件放在所选目录中。

// Getting path to store the file   
String path = root.getAbsolutePath();   
path += curr.getName();   
request.setDestinationInExternalPublicDir(path, new File(url).getName());    
// get download service and enqueue file   
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);     
manager.enqueue(request);   

The String named url, that is being passed to the File constructor, contains a URL. 名为url的String,传递给File构造函数,包含一个URL。 Just using that to retrieve that name of the file. 只需使用它来检索该文件的名称。

UPDATE: I just found the file. 更新:我刚刚找到了该文件。 Its in the phone not the sd card. 它在手机里不是SD卡。 It was in this folder, storage\\emulated\\00. 它位于此文件夹中,storage \\ emulated \\ 00。 Not sure why? 不知道为什么? Also, the absolute path that I'm getting is storage\\emulated\\0. 另外,我得到的绝对路径是storage \\ emulated \\ 0。

So, I figured out that the path returned is always storage\\emulated\\0 for the call getExternalStorageDirectory().getPath()) to the Environment. 因此,我发现返回的路径始终是storage\\emulated\\0用于调用getExternalStorageDirectory().getPath())到环境。 Which is what I using to the get the initial directory. 这是我用来获取初始目录的内容。 So for instance if I navigated through the folders to the Downloads folder the path would be storage\\emulated\\0\\Download . 因此,例如,如果我浏览文件夹到Downloads文件夹,则路径将是storage\\emulated\\0\\Download When I was downloading the file though instead of going to the actual downloads folder it would go to, or create, the downloads folder in the emulated folder in the storage folder. 当我下载文件而不是去实际的下载文件夹时,它会转到或创建存储文件夹中模拟文件夹中的下载文件夹。 To resolve this I found the index of the zero in the path, added 1, and got the substring using that. 为了解决这个问题,我在路径中找到了零的索引,添加了1,并使用它得到了子串。 After that it worked. 之后它起作用了。 Also I had to do this in another method where I navigate through the directory. 另外,我必须在另一种浏览目录的方法中执行此操作。 As a parameter I pass in the file and then within the method I get the substring. 作为参数,我传入文件,然后在方法中我得到子字符串。 Within the method that I use to download the file I get storage\\emulated\\0 no matter what. 在我用来下载文件的方法中,无论如何都得到storage\\emulated\\0 Not sure why it does this. 不知道为什么会这样做。 If anyone could explain this it would be greatly appreciated. 如果有人能解释这一点,将不胜感激。

Does root.getAbsolutePath() return a string with a path separator character at the end? root.getAbsolutePath()是否返回末尾带有路径分隔符的字符串?

If not, then you might be doing something like bin/usr/var/appfilename.ext instead of bin/usr/var/app/filename.ext , because you're concatenating it straight into name of the file. 如果没有,那么你可能会使用bin/usr/var/appfilename.ext而不是bin/usr/var/app/filename.ext ,因为你将它直接连接到文件名。

I still had this issue on Android 7. In my case, the file would not show up until I restart the device . 我在Android 7上仍然存在这个问题。在我的情况下,直到我重启设备才会显示该文件。 This a known issue: https://issuetracker.google.com/issues/36956498 这是一个已知问题: https//issuetracker.google.com/issues/36956498

Source: https://stackoverflow.com/a/20413888/2552811 来源: https//stackoverflow.com/a/20413888/2552811

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM