1

I have implemented the OneDrive picker in my app to allow the user to easily select a file from the OneDrive cloud storage. My app then gets the weblink that points to the file. I would now like to download (make a copy) of the file to the local storage on the Android device.

I would have thought this would be simple (As Dropbox made downloading a local copy part of their picker function) but it seems OneDrive has not. I've done some searches to find out how to download a file give the weblink, but they either seem way to complicated or have not worked.

Below is the code for the onactivity result from the OneDrive picker. Now that I have the Uri, please help me with how to get a local copy of the file so that I can use it. Thanks!

if (requestCode == ONEDRIVE_CHOOSER_REQUEST) 
{ 
     // Get the results from the picker
    IPickerResult result = mPicker.getPickerResult(requestCode, resultCode, data);

    // Handle the case if nothing was picked
    if (result != null) {
        // Do something with the picked file

          Uri fileUri = result.getLink();
gbotha
  • 1,231
  • 17
  • 23
  • You can use the OneDrive picker to get a DownloadLink, which gives you a direct link to the file. We've updated the sample to show how this can be done, https://github.com/OneDrive/onedrive-picker-android . Why would your program request a web view link if you just want to download the file? – Peter Nied Nov 13 '14 at 19:49
  • Thanks Peter. Yes, I ended up using the DownloadLink and then made a local copy of the file. Thanks, I'll take a look at the github info you sent too. – gbotha Nov 15 '14 at 03:31

2 Answers2

0

Okay, third time was the charm. Although I really was surprised there isn't and easier way to download a file. If anyone has a simpler solution, I'd love to hear it.

I followed some advice from this post How to download and save a file from Internet using Java? and have managed to download the file from OneDrive.

    public static void download(String address, String localFileName) {
    OutputStream out = null;
    URLConnection conn = null;
    InputStream in = null;

    try {
        URL url = new URL(address);
        out = new BufferedOutputStream(new FileOutputStream(localFileName));
        conn = url.openConnection();
        in = conn.getInputStream();
        byte[] buffer = new byte[1024];

        int numRead;
        long numWritten = 0;

        while ((numRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, numRead);
            numWritten += numRead;
        }

        System.out.println(localFileName + "\t" + numWritten);
    } 
    catch (Exception exception) { 
        exception.printStackTrace();
    } 
    finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } 
        catch (IOException ioe) {
        }
    }
Community
  • 1
  • 1
gbotha
  • 1,231
  • 17
  • 23
0

I don't know what URL the picker gives you but doing this via OneDrive web you get a link which just shows the File. But you can translate this link to get a direct download link:

The link you get:

https://onedrive.live.com/redir?resid=8F99649728BEB2F3%212780

You just need to replace redir by download:

https://onedrive.live.com/download?resid=8F99649728BEB2F3%212780

Reference: How to get direct download link from OneDrive

But you should keep in mind that this is no official way (at least I couldn't find this information on a Microsoft website) so it's possible that Microsoft changes this and your links don't work anymore.

Kai
  • 38,985
  • 14
  • 88
  • 103