android dropbox文件夹,Android的Dropbox Sync API-更新缓存的文件

该代码段展示了一个Android应用中实现Dropbox文件下载的逻辑,通过检查文件的修改时间和大小来决定是否需要同步。它使用DbxFile对象进行文件状态检查,并在文件更新时进行等待和重新同步,确保获取到最新的文件版本。如果本地文件过时或不存在,代码将从Dropbox下载并保存到本地。
摘要由CSDN通过智能技术生成

这是我尝试获取最新文件的尝试,但是正如我在对您的问题的评论中所说的那样,似乎有时有时必须进行两次同步调用才能获取最新文件.

fileModified和fileSize比较比较粗糙,但似乎可以解决问题.至少比我到目前为止发现的要好.

public DropboxFileDownloader() {

super("FileDownloader");

}

@Override

protected void onHandleIntent(Intent intent) {

String turiosHome = intent.getStringExtra(Constants.EXTRA_HOME);

String fileName = intent.getStringExtra(Constants.EXTRA_FILENAME);

String folderPath = intent.getStringExtra(Constants.EXTRA_FOLDERPATH);

ResultReceiver receiver = intent.getParcelableExtra(Constants.EXTRA_RECEIVER);

Bundle bundle = new Bundle();

String fullpath = folderPath + "/" + fileName;

DbxFile file;

long fileModified = 0;

long fileSize = 0;

try {

file = dbxFs.open(new DbxPath(fullpath));

try {

DbxFileStatus fileStatus = 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 Edit

try

{

//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;

}

File stored_dir = new File(turiosHome + "/" + folderPath);

if (!stored_dir.exists()) {

stored_dir.mkdirs();

}

File stored_file = new File(turiosHome + "/" + folderPath,

fileName);

// File stored_file = getFileStreamPath(fileName);

long local_modified = stored_file.lastModified();

long local_size = stored_file.length();

boolean should_sync = (fileModified > local_modified)

|| fileSize != local_size;// && Math.abs(fileModified -

// local_modified) >

// TimeUnit.MILLISECONDS.convert(1,

// TimeUnit.MINUTES);

boolean fileexists = stored_file.exists();

if (should_sync || !fileexists) {

InputStream inputStream = null;

FileOutputStream out = null;

try {

// read this file into InputStream

inputStream = file.getReadStream();

out = new FileOutputStream(stored_file);

int read = 0;

byte[] bytes = new byte[1024];

int bytes_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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值