1

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();
        }
    }
}
1
  • Please add some more relevant tags like 'Android', 'Java', 'DropboxAPI' etc. You'll increase your chances of getting an answer quick as some users are following tags of their own expertise..
    – Bart
    Commented Jul 8, 2015 at 12:15

2 Answers 2

1

If the file exists, then the Entry is not null

 public boolean isExists(String path) {
            boolean ret = false;
            try {
                Entry existingEntry = metadata(path, 1, null, false, null);
                if (existingEntry != null) {
                    ret = true;
                }
            } catch (DropboxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ret = false;
            }
            return ret;
        }
0
0
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();

    }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.