且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查文件是否已经存在于Dropbox的

更新时间:2023-09-27 22:36:28

 私人无效loadFiles(最后弦乐目录){
        新的Thread(){
            @覆盖
            公共无效的run(){

                字符串的mpath =目录;
                进入direntEx = NULL;
                尝试 {
                    direntEx = mApi.metadata(的mpath,1000,空,真,空);
                }赶上(DropboxException E){
                    e.printStackTrace();
                }

                如果(direntEx.contents.size()!= 0){
                    对于(输入ENT:direntEx.contents){
                        字符串名称= ent.fileName();
                          / *这里比较文件* /

                   }
                 }

                super.run();
            }

        }。开始();

    }
 

I am using following code to upload a file to Dropbox. But I want to check if the file exists on Dropbox already, to avoid duplications. So how can I check if a file already exists or not? As I am new to Android, I don't know what to do now

public class UploadFileToDropbox extends AsyncTask<Void, Void, Boolean>
{

    private DropboxAPI<?> dropbox;
    private String path;
    private Context context;

    public UploadFileToDropbox(Context context, DropboxAPI<?> dropbox,
                               String path) {
        this.context = context.getApplicationContext();
        this.dropbox = dropbox;
        this.path = path;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        final File tempDir = context.getCacheDir();
        File tempFile;
        FileWriter fr;
        try {
            tempFile = File.createTempFile("file", ".txt", tempDir);
            fr = new FileWriter(tempFile);
            fr.write("Test file uploaded using Dropbox API for Android");
            fr.close();

            FileInputStream fileInputStream = new FileInputStream(tempFile);
            dropbox.putFile(path + "sample.txt", fileInputStream,
                    tempFile.length(), null, null);
            tempFile.delete();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DropboxException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            Toast.makeText(context, "File Uploaded Successfully!",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "Failed to upload file", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

private void loadFiles(final String directory) {
        new Thread() {
            @Override
            public void run() {

                String mPath = directory;
                Entry direntEx = null;
                try {
                    direntEx = mApi.metadata(mPath, 1000, null, true, null);
                } catch (DropboxException e) {
                    e.printStackTrace();
                }

                if (direntEx.contents.size() != 0) {
                    for (Entry ent : direntEx.contents) {
                        String name = ent.fileName();
                          /*Compare file here*/

                   }
                 }

                super.run();
            }

        }.start();

    }