woojoo666
woojoo666

Reputation: 7921

How to detect if file exists in Google Drive (and not trashed) using Java?

Pretty self-explanatory title. I'm using Google Drive Client Api for Java. What I currently have is as follows:

File f = mService.files.get(fileId).execute();

However, I can't find the property in File used to check if a file is trashed or not. File.getExplicitlyTrashed() gives me null for both trashed and not trashed files.

Upvotes: 5

Views: 3158

Answers (1)

woojoo666
woojoo666

Reputation: 7921

The trashed property is hidden inside the File.Labels class, which you can get from File.getLabels(). A working example is:

public boolean validFileId(String id) {
    try {
        File f = mService.files().get(id).execute();
        return !f.getLabels().getTrashed();
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("bad id: " + id);
    }
    return false;
}

Upvotes: 3

Related Questions