I’m building an Android app that needs to store user data in Google Drive’s app data folder. I’ve already set up everything in Google Cloud Console including the consent screen, OAuth credentials for Android and web, and enabled the Drive API.
Authentication and authorization are working fine using the AuthorizationClient API. However, when I try to upload a JSON file to the app data folder, I keep getting a FileNotFoundException.
Here’s my code for creating the file:
private void uploadToAppDataFolder(AuthorizationResult authResult) {
GoogleCredentials creds = GoogleCredentials.create(
new AccessToken(authResult.getAccessToken(), null));
Drive driveService = new Drive.Builder(
new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
new HttpCredentialsAdapter(creds))
.setApplicationName(getString(R.string.app_name))
.build();
try {
File metadata = new File();
metadata.setName("settings.json");
metadata.setParents(Collections.singletonList("appDataFolder"));
java.io.File localFile = new java.io.File("data/settings.json");
FileContent content = new FileContent("application/json", localFile);
File uploadedFile = driveService.files().create(metadata, content)
.setFields("id")
.execute();
Log.d(TAG, "Uploaded file ID: " + uploadedFile.getId());
} catch (IOException ex) {
ex.printStackTrace();
}
}
The error I’m getting is:
java.io.FileNotFoundException: data/settings.json: open failed: ENOENT (No such file or directory)
What could be causing this issue? Any ideas on how to fix it?