cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating a folder in Dropbox

Creating a folder in Dropbox

Robert S.138
Helpful | Level 7

Is there a sample of creating a folder in Dropbox from the Android Java SDK for Core API v2?  The example of uploading files seems to deal only with the root folder.  I have a background task that has an instance of DbxUserFilesRequests.  I know that applying createFolder() to this instance will do it, but if the folder already exists, it throws an exception.  How can I find out if the folder exists before attempting to create it?

 

3 Replies 3

Greg-DB
Dropbox Staff

Hi Robert, just trying to create the folder and catching the exception is a fine way to handle this. It's as efficient as possible too, since it just requires the one API call.

If you do want to check ahead of time, you can use getMetadata. That will throw an exception if the folder doesn't exist though anyway, which you would need to catch.

Robert S.138
Helpful | Level 7

Is there a specific exception I should check for on the createFolder?  I don't want to ignore an exception that is unrelated to the fact that the folder already exists.

Greg-DB
Dropbox Staff

Yes, API v2 offers structured exceptions, so you can detect specific scenarios like this. The documentation for createFolder covers all of the types of exceptions it can throw.

For example, to detect this particular error case:

try {
FolderMetadata folder = client.files().createFolder("/test_java_createFolder");
System.out.println(folder.getName());
} catch (CreateFolderErrorException err) {
if (err.errorValue.isPath() && err.errorValue.getPathValue().isConflict()) {
System.out.println("Something already exists at the path.");
} else {
System.out.print("Some other CreateFolderErrorException occurred...");
System.out.print(err.toString());
}
} catch (Exception err) {
System.out.print("Some other Exception occurred...");
System.out.print(err.toString());
}

 

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Robert S.138 Helpful | Level 7
What do Dropbox user levels mean?