Search code examples
javaandroiddownloaddropboxdropbox-api

How to download certain file using Dropbox API V2?


I need to download certain file/folder from Dropbox using DbxClientV2. I know the path to this file and it will always be the same so I don't want let user to pick it. I've seen:

public void onFileClicked(final FileMetadata file) { }

But it seems this isn't what I'm looking for and I don't know how to create FileMetadata object using file path.
I can't find anything helpful. I've got sample class from GitHub but it still requires FileMetadata.


Solution

  • Okay, it took me some time so I post my answer for others.

    Of course you have to specify the place where you want to save it (but that's obvious) then create OutputStream:

    String path = Environment.getExternalStorageDirectory().toString() + "/DCIM";
                    File file = new File(path, "test.txt");
    
                    FileOutputStream outputStream = null;
                    try {
                        outputStream = new FileOutputStream(file);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
    

    After this we create Metadata object and initialize it and then download it using metadata.getPathLower().

     try {
                        Metadata pathMetadata = client.files().getMetadata("/test.txt");
                        client.files().download(pathMetadata.getPathLower()).download(outputStream);
                        Log.e("METADATA", pathMetadata.toString());
    
                    } catch (DbxException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    

    The strange this is that the getPathLower() returns "test.txt" but when we type it raw inside download() method it returns

    java.lang.IllegalArgumentException: String 'path' does not match pattern
    

    ("/test.txt" doesn't work too).

    It took me much time to make it work so I hope my answer helps someone to save this time.

    Happy coding!