6

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, donescamillo@gmail.com

user1523271
  • 1,005
  • 2
  • 13
  • 27

3 Answers3

1

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.

Neeraj Nama
  • 1,562
  • 1
  • 18
  • 24
0

May you an build your URI with the download folder : Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toURI();

earlypearl
  • 596
  • 6
  • 10
  • 1
    This is what I did, but it works only in Android 5, not Android 6. I found a way around it-I cant get good URI, but I can open the just-downloaded file. So I open the file and copy it wherever I want – user1523271 Nov 05 '15 at 11:38
0

It's work. @2016.6.24

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals( action)) {
        DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
        long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadId);
        Cursor c = downloadManager.query(query);
        if(c != null) {
            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                    String downloadFileUrl = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    startInstall(context, Uri.parse(downloadFileUrl));
                }
            }
            c.close();
        }
    }
}




private boolean startInstall(Context context, Uri uri) {
    if(!new File( uri.getPath()).exists()) {
        System.out.println( " local file has been deleted! ");
        return false;
    }
    Intent intent = new Intent();
    intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction( Intent.ACTION_VIEW);
    intent.setDataAndType( uri, "application/vnd.android.package-archive");
    context.startActivity( intent);
    return true;
}
qinmiao
  • 5,559
  • 5
  • 36
  • 39