Dropbox Sync Api Android - Updating Cached Files
Solution 1:
Here's how the Sync API works:
- It constantly syncs metadata (what files exist and their revisions) and notifies you via listeners you set up on the file system.
- For open files, it downloads any newer version of the file available and notifies you via the listener you set up on the file itself.
So if you want to get the latest version of a file, you need to open the file and hold it open while waiting for the listener to notify you that the newer version of the file is cached. Then you can call update
to get access to that new data.
EDIT: Pasting code from https://www.dropbox.com/developers/sync/start/android#listeners:
DbxFileStatusstatus= testFile.getSyncStatus();
if (!status.isCached) {
testFile.addListener(newDbxFile.Listener() {
@OverridepublicvoidonFileChange(DbxFile file) {
// Check testFile.getSyncStatus() and read if it's ready
}
});
// Check if testFile.getSyncStatus() is ready already to ensure nothing// was missed while adding the listener
}
Solution 2:
Here is my attempt to get the latest file but as I stated in the comment to your question, it seems I sometimes have to do two sync calls in order to get the latest file.
The fileModified and fileSize comparison is rather crude but seems to do the trick. Better than what I have found so far at least.
publicDropboxFileDownloader() {
super("FileDownloader");
}
@OverrideprotectedvoidonHandleIntent(Intent intent) {
StringturiosHome= intent.getStringExtra(Constants.EXTRA_HOME);
StringfileName= intent.getStringExtra(Constants.EXTRA_FILENAME);
StringfolderPath= intent.getStringExtra(Constants.EXTRA_FOLDERPATH);
ResultReceiverreceiver= intent.getParcelableExtra(Constants.EXTRA_RECEIVER);
Bundlebundle=newBundle();
Stringfullpath= folderPath + "/" + fileName;
DbxFile file;
longfileModified=0;
longfileSize=0;
try {
file = dbxFs.open(newDbxPath(fullpath));
try {
DbxFileStatusfileStatus= file.getNewerStatus();
if (fileStatus != null && !fileStatus.isLatest) {
/*while (file.getNewerStatus().pending == PendingOperation.DOWNLOAD) {
Log.d(TAG, "Waiting for " + fileName + " to be downloaded");
Thread.sleep(1000);
}*/if (fileStatus.isCached) {
//Start of Edittry
{
//Running this do while loop until the Latest version of this file is cached.do
{
Log.d(TAG, "Updating the existing file !");
//Updating the file
file.update();
while (file.getNewerStatus().pending ==PendingOperation.DOWNLOAD)
{
Log.d(TAG, "Waiting for " + fileName+ " to be downloaded");
Thread.sleep(1000);
}
} while (fileStatus.isLatest);
}
catch (Exception dBException)
{
Log.e(TAG, "Error while getting newer Status !, Error = "+dBException.toString());
dBException.printStackTrace();
}
//End of Edit
}
}
fileModified = file.getInfo().modifiedTime.getTime();
fileSize = file.getInfo().size;
} catch (DbxException e) {
Log.e(TAG, e.getMessage(), e);
bundle.putString(Constants.EXTRA_MESSAGE, e.getMessage());
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_ERROR, bundle);
return;
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InvalidPathException e1) {
Log.e(TAG, e1.getMessage(), e1);
bundle.putString(Constants.EXTRA_MESSAGE, e1.getMessage());
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_ERROR, bundle);
return;
} catch (DbxException e1) {
Log.e(TAG, e1.getMessage(), e1);
bundle.putString(Constants.EXTRA_MESSAGE, e1.getMessage());
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_ERROR, bundle);
return;
}
Filestored_dir=newFile(turiosHome + "/" + folderPath);
if (!stored_dir.exists()) {
stored_dir.mkdirs();
}
Filestored_file=newFile(turiosHome + "/" + folderPath,
fileName);
// File stored_file = getFileStreamPath(fileName);longlocal_modified= stored_file.lastModified();
longlocal_size= stored_file.length();
booleanshould_sync= (fileModified > local_modified)
|| fileSize != local_size;// && Math.abs(fileModified -// local_modified) >// TimeUnit.MILLISECONDS.convert(1,// TimeUnit.MINUTES);booleanfileexists= stored_file.exists();
if (should_sync || !fileexists) {
InputStreaminputStream=null;
FileOutputStreamout=null;
try {
// read this file into InputStream
inputStream = file.getReadStream();
out = newFileOutputStream(stored_file);
intread=0;
byte[] bytes = newbyte[1024];
intbytes_counter=0;
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
bytes_counter++;
}
Log.d(TAG, "Wrote: " + file.getPath().getName() + " "
+ bytes_counter + " kb");
if (!fileexists) {
bundle.putString(Constants.EXTRA_FILEPATH, fullpath);
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_CREATED, bundle);
} else {
bundle.putString(Constants.EXTRA_FILEPATH, fullpath);
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_UPDATED, bundle);
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
bundle.putString(Constants.EXTRA_MESSAGE, e.getMessage());
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_ERROR, bundle);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
bundle.putString(Constants.EXTRA_MESSAGE, e.getMessage());
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_ERROR, bundle);
}
}
}
else {
bundle.putString(Constants.EXTRA_FILEPATH, fullpath);
receiver.send(DropboxFileDownloaderResultReceiver.RESULTCODE_UPTODATE, bundle);
}
file.close();
}
}
Solution 3:
I can't found working example for me. But for my case - i don't need cashed version of files - only newest (data synchronization between devices).
I used
mDbFileSystem.setMaxFileCacheSize(0);
All or nothing. But now nessasary thread for downloading in background.
Post a Comment for "Dropbox Sync Api Android - Updating Cached Files"