Pregunta

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. Just using that to retrieve that name of the file.

UPDATE: I just found the file. Its in the phone not the sd card. It was in this folder, storage\emulated\00. Not sure why? Also, the absolute path that I'm getting is storage\emulated\0.

¿Fue útil?

Solución

So, I figured out that the path returned is always storage\emulated\0 for the call getExternalStorageDirectory().getPath()) to the Environment. 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. 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. 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. Not sure why it does this. If anyone could explain this it would be greatly appreciated.

Otros consejos

Does root.getAbsolutePath() return a string with a path separator character at the end?

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.

I still had this issue on Android 7. In my case, the file would not show up until I restart the device. This a known issue: https://issuetracker.google.com/issues/36956498

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top