Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 6 get path to downloaded file

I our app (Xamarin C#) we download files from a server. At the end of a succeful download we get the URI to the newly-downloaded file and from the URI we get the file path:

Android.Net.Uri uri = downloadManager.GetUriForDownloadedFile(entry.Value);
path = u.EncodedPath;

In Android 4.4.2 and in Android 5 the uri and path look like this:

uri="file:///storage/emulated/0/Download/2.zip"
path = u.EncodedPath ="/storage/emulated/0/Download/2.zip"

We then use path to process the file. The problem is that in Android 6 (on a real Nexus phone) we get a completely different uri and path:

uri="content://downloads/my_downloads/2802"
path="/my_downloads/2802"

This breaks my code by throwing a FileNotFound exception. Note that the downloaded file exists and is in the Downloads folder. How can I use the URI I get from Android 6 to get the proper file path so I can to the file and process it?

Thank you, [email protected]

like image 564
user1523271 Avatar asked Oct 24 '15 07:10

user1523271


1 Answers

I didn't get your actual requirement but it looks like you want to process file content. If so it can be done by reading the file content by using file descriptor of downloaded file. Code snippet as

    ParcelFileDescriptor parcelFd = null;
    try {
        parcelFd = mDownloadManager.openDownloadedFile(downloadId);
        FileInputStream fileInputStream = new FileInputStream(parcelFd.getFileDescriptor());
    } catch (FileNotFoundException e) {
        Log.w(TAG, "Error in opening file: " + e.getMessage(), e);
    } finally {
        if(parcelFd != null) {
            try {
                parcelFd.close();
            } catch (IOException e) {
            }
        }
    }

But I am also looking to move or delete that file after processing.

like image 117
Neeraj Nama Avatar answered Oct 14 '22 03:10

Neeraj Nama