简体   繁体   中英

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 but it still requires FileMetadata .示例类,但它仍然需要FileMetadata

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM