Search code examples
javagoogle-apigoogle-drive-apigoogle-api-java-client

FileNotFoundException when CREATING a google drive file


I have an app on the google with firebase set up and users can log in to the app and I want to be able to store app data.

The GoogleSignInOptions is set as follows

GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
            .requestEmail()
            .requestScopes(Scope(Scopes.DRIVE_APPFOLDER))
            .build()

And the createFile method is

public Task<String> createFile() {
        return Tasks.call(mExecutor, () -> {
            File fileMetadata = new File();
            fileMetadata.setName("profile.json");
            fileMetadata.setParents(Collections.singletonList("appDataFolder"));

            java.io.File filePath = new java.io.File("profile.json");

            FileContent mediaContent = new FileContent("application/json", filePath);

            File file = mDriveService.files().create(fileMetadata, mediaContent)
                    .setFields("id")
                    .execute();

            return file.getId();
        });
    }

but fore some reason, calling the createFile method throws the following exception

java.io.FileNotFoundException: profile.json (No such file or directory)

why ? for some more context, this is tested in the debug version of the app


Solution

  • Turns out the problematic line was

     java.io.File filePath = new java.io.File("profile.json");
    

    I needed to put the real path of the file and not just "profile.json"