android - Dropbox Sync API Android - 更新缓存文件

标签 android dropbox synchronization

我在更新我的 Android 应用程序中的现有缓存文件时遇到问题。

for(DbxFileInfo fInfo : fileList)
{
    Log.d(TAG, "File Path = "+fInfo.path.toString());
    String fileName = fInfo.path.getName().trim();

    try
    {
        DbxPath tempFilePath    = new DbxPath(fInfo.path.toString());
        DbxFile tempFile        = mDbFileSystem.open(tempFilePath);

        if(tempFile.getSyncStatus().isCached)
        {
            Log.v(TAG, "File is already cached !");

            if(tempFile.getSyncStatus().isLatest)
            {
                Log.v(TAG, "File's Latest Version is Cached !");
            }
            else
            {
                Log.v(TAG, "File's Latest Version is not Cached !");
            }
        }

        try
        {
            tempFile.getNewerStatus();
        }
        catch(Exception dBException)
        {
            Log.e(TAG, "Error while getting newer Status !");
        }

        InputStream input       = new BufferedInputStream(tempFile.getReadStream());
        OutputStream output     = new FileOutputStream(cntx.getFilesDir() + "/SyncedData/" + fileName);

        byte data[] = new byte[1024];
        int count;

        //total size is in Bytes
        while ((count = input.read(data)) != -1)
        {
            totalBytesDownloaded += count;
            publishProgress((int) (totalBytesDownloaded * 100/totalFileSize));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
        tempFile.close();
        mDbFileSystem.delete(tempFile.getPath());

        result = true;
    }
    catch(Exception e)
    {
        Log.e(TAG, "Error occured while downloading files !, Error = "+e.toString());
        result = false;
    }
}

我将同名的不同文件放在我的 Synced Dropbox 文件夹中,下载它们后我得到的是旧版本的文件。
有什么方法可以更新我现有的缓存文件或清除 Dropbox 缓存(在我的应用程序中) 非常感谢任何帮助,谢谢。

最佳答案

同步 API 的工作原理如下:

  • 它不断同步元数据(存在哪些文件及其修订)并通过您在文件系统上设置的监听器通知您。
  • 对于打开的文件,它会下载可用文件的任何更新版本,并通过您在文件本身上设置的监听器通知您。

所以如果你想获得一个文件的最新版本,你需要打开文件并保持打开状态,同时等待监听器通知你文件的更新版本已被缓存.然后您可以调用 update 来访问该新数据。

编辑:粘贴来自 https://www.dropbox.com/developers/sync/start/android#listeners 的代码:

DbxFileStatus status = testFile.getSyncStatus();
if (!status.isCached) {
    testFile.addListener(new DbxFile.Listener() {
        @Override
        public void onFileChange(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
}

关于android - Dropbox Sync API Android - 更新缓存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20948156/

相关文章:

android - 使用更新的 JDK Ionic 2 重新发布 android 应用程序

android - 在 Android 中显示来自服务/接收器的 OK 对话框

css - 从 Markdown 文件自动创建迷你网站?

c# - 如何在C#中存储https POSTmethod的返回值

c - 一次只有一个进程必须执行一段代码

php - 我应该如何处理 PHP 中的查询同步?

android - 使用 SimpleOnScaleGestureListener 在 Fragment 中的 EditText 上捏合缩放

android - 强制操作栏项目溢出

Git fatal error : Reference has invalid format: 'refs/heads/master

java - 使用 System.out.println(Thread.currentThread().getName() + ""+count);导致同步