Prevent duplicate folder creation in Google Drive using Android API

I’m trying to set up a system where my Android app can create folders on Google Drive, but I’m running into an issue. Every time I launch the app, it makes a new folder with the same name. I want to figure out a way to check if the folder already exists before creating another one. Here’s my current code:

@Override
public void onConnected(Bundle data) {
    Log.d(TAG, "Connection successful.");
    FolderMetadata metadata = new FolderMetadata.Builder()
            .setName("MyAppFolder").build();
    DriveResource.DriveRootFolder.createFolder(driveClient, metadata)
            .addOnCompleteListener(this::handleFolderCreation);
}

private void handleFolderCreation(Task<DriveFolder> task) {
    if (task.isSuccessful()) {
        DriveFolder newFolder = task.getResult();
        Log.d(TAG, "Folder created: " + newFolder.getDriveId());
    } else {
        Log.e(TAG, "Folder creation failed", task.getException());
    }
}

Any suggestions on how to ensure a folder isn’t created twice would be appreciated. Thanks!

hey dancingfox, i had the same issue. what worked for me was checking if the folder exists first. you can use the Files.list() method to search for your folder name. if it doesn’t exist, then create it. if it does, just use that one. saves alot of headache!

Consider implementing a caching mechanism to save the folder ID once it is created. This approach allows your app to check the cache first instead of making an additional API call every time it needs to access the folder. For example, you could use SharedPreferences to store the folder ID so that before attempting folder creation, you check if the ID already exists. If it is available, you simply use that folder; if not, you create a new one and update the stored value. This reduces overhead and streamlines folder management.

I’ve encountered this problem in one of my projects before. The solution I found was to attempt a query for the folder with the name you’re interested in before triggering a creation command. Essentially, you perform a query for any folder that matches the name, and if the query returns one or more results, then you can be confident the folder exists already. If no folders come back, you can proceed to create a new one.

For example:

Query query = new Query.Builder()
    .addFilter(Filters.eq(SearchableField.TITLE, "MyAppFolder"))
    .addFilter(Filters.eq(SearchableField.MIME_TYPE, DriveFolder.MIME_TYPE))
    .build();

driveClient.query(query).addOnSuccessListener(metadataBuffer -> {
    if (metadataBuffer.getCount() > 0) {
        // Folder exists, use it
        DriveFolder existingFolder = metadataBuffer.get(0).getDriveId().asDriveFolder();
    } else {
        // Create new folder
        createNewFolder();
    }
    metadataBuffer.release();
}).addOnFailureListener(e -> {
    Log.e(TAG, "Error querying for folder", e);
});

This method helps ensure you avoid creating duplicate folders and keeps your Drive organized. I hope this provides some clear guidance.