繁体   English   中英

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

[英]Dropbox Sync API Android - Updating Cached Files

我在更新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
}

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

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();
}
}

我找不到适合我的工作示例。 但就我而言-我不需要现金的文件版本-仅是最新的(设备之间的数据同步)。

我用了

mDbFileSystem.setMaxFileCacheSize(0);

全部或全无。 但是现在nessasary线程可以在后台下载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM