0

https://developers.google.com/drive/api/v3/appdata#create_a_file_in_the_application_data_folder here is an example how to create a file using Google Drive API on the appData folder. I didn't find any example on how to create a txt file and manipulate it's content using the Google Drive API.

Any help is appreciated.

Mohammad Haidar
  • 1,109
  • 1
  • 16
  • 35
  • Your question is tagged with [tag:google-drive-android-api] which has been deprecated. Do you mean just [tag:google-drive-api]? Are you looking for an answer in a specific programming language or would you be able to adapt a sample snippet into your language of choice yourself? – ziganotschka Jul 20 '20 at 12:50
  • @ziganotschka yeah i mean google-drive-api and am looking for an answer in java/android form – Mohammad Haidar Jul 20 '20 at 13:29

1 Answers1

0

To create a txt filte in the appData folder

Modify the Name and filePath of sample code snippet in the documentation from json to txt and the mimeType from application/json to text/plain:

File fileMetadata = new File();
fileMetadata.setName("config.txt");
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
java.io.File filePath = new java.io.File("files/config.txt");
FileContent mediaContent = new FileContent("text/plain", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id")
    .execute();
System.out.println("File ID: " + file.getId());

To manipulate the content of the file

See here for official information on how to upload the content.

Basically, for a single request you need to

  • obtain the resumable URI
  • perfom PUT request to it containing the file content in the request body
  • Specify the content length (mediaContent.setLength())

I do not have a Java snippet for this part, but I have done it before in Apps Script - I hope this is helpful to understand how you can manipulate the content of the file you crated in the appData fodler.

See also here for a detailed sample on how to upload a file with content in Java.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • this is an example right from the documentation, you just changed config.json into config.txt, running this code will result an FileNotFoundException – Mohammad Haidar Jul 21 '20 at 14:12
  • I also changed the data type and the path. Which line gives you the "File not found" error? If you are trying to retrieve the file with `Files:get` - this is not possible since the appData folder is not visible or accessible. – ziganotschka Jul 21 '20 at 14:42