I'm trying to take Dropbox V2 API into use for uploading my application datafiles (SQLite and preferences) from my Android app into my Dropbox account. I can successfully start the client:
// Dropbox authentication
config = new DbxRequestConfig("dropbox/liikennevalot");
client = new DbxClientV2(config, ACCESS_TOKEN);
My app says the link is up, listing my user credentials:
FullAccount account = client.users().getCurrentAccount();
I can list the files in my app Dropbox directory:
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
Log.d("DROPBOX", metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
But when trying to upload from Android to the Dropbox app root directory with
try (InputStream in = new FileInputStream(getExternalFilesDir(null) + "/" + "testi.txt")) {
FileMetadata metadata = client.files().uploadBuilder("")
.withMode(WriteMode.OVERWRITE)
.uploadAndFinish(in);
}
I get an exception
java.lang.IllegalArgumentException: String 'path' does not match pattern
which points to the code line with the client.files().uploadBuilder("")...
Examining the exception stack, I find that in Dropbox API V2, class CommitInfo has a constructor, which is giving me this exception
} else if(!Pattern.matches("(/(.|[\\r\\n])*)|(ns:[0-9]+(/.*)?)", path)) {
throw new IllegalArgumentException("String \'path\' does not match pattern");
I do not know enough Java REGEX to make any sense from that REGEX, so I really don't know what is causing me this exception. Dropbox API V2 documentation says that "" represents the application's root folder in Dropbox, so my client.files().uploadBuilder("")... should be perfecty valid code.
I have also tried to create a subdirectory "test" under my application's Dropbox directory, and change the code to client.files().uploadBuilder("test")... but I get the same exception every time.
Can anyone help me interpreting that REGEX so that would give me a clue as to what is wrong with my code?
The parameter you're passing to uploadBuilder
should be the path inside Dropbox where you want to upload the file. You're passing in ""
which is the root itself, which isn't an allowed location for a file upload, since it doesn't contain a file name.
Instead, you should pass in a value like "/test.txt"
, which would tell Dropbox to upload the contents as a file named "test.txt" in root.
Or, for example, if you specify "/Documents/test.txt"
, it would upload as a file named "test.txt" in a folder named "Documents".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With