Search code examples
javaandroidgoogle-drive-android-api

Updating only metadata of existing Google Drive file Using Java


I want to only update the meta data(for example description or file name) of existing Google drive file, There are some documention to do so using javascript Google Drive Update Documentation . But no Java documentation. Also i found code to do this

 private static File updateFile(Drive service, String fileId, String newTitle,
      String newDescription, String newMimeType, String newFilename, boolean newRevision) {
    try {
      // First retrieve the file from the API.
      File file = service.files().get(fileId).execute();

      // File's new metadata.
      file.setTitle(newTitle);
      file.setDescription(newDescription);
      file.setMimeType(newMimeType);

      // File's new content.
      java.io.File fileContent = new java.io.File(newFilename);
      FileContent mediaContent = new FileContent(newMimeType, fileContent);

      // Send the request to the API.
      File updatedFile = service.files().update(fileId, file, mediaContent).execute();

      return updatedFile;
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
      return null;
    }
  }

  // ...
}

But in this it also upadtes content(which i dont want to do). How can i achive this?


Solution

  • After doing more research i found the solution,

       File file =new File();
         file.setName("new name");
         service.files().update(fileid, file).execute();
    

    This code will update the name of the file.