Android FileNotFoundException while uploading JSON to Google Drive App Data folder

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?

You’re getting a FileNotFoundException because “data/settings.json” doesn’t exist in your app’s file system. Android looks for this file in your app’s working directory, which usually doesn’t have a “data” folder. If you need a local file, use Android’s getFilesDir() or getCacheDir() methods to get the right file paths. Better yet, just generate your JSON content in-memory and use InputStreamContent to upload it directly. This skips the file system entirely and avoids permission headaches.

yea, looks like file path is wrong. use getFilesDir() to verify if “data/settings.json” exists. alternatively, maybe hold the JSON in memory and upload from there to avoid this issue.

You’re getting that FileNotFoundException because “data/settings.json” doesn’t exist on the device. Android needs proper file system paths, but there’s an easier way. Skip creating a local file altogether - just upload your JSON directly with ByteArrayContent: java String jsonContent = "{\"key\": \"value\"}"; // your JSON data ByteArrayContent content = new ByteArrayContent("application/json", jsonContent.getBytes()); File uploadedFile = driveService.files().create(metadata, content) .setFields("id") .execute(); This uploads straight to the app data folder without dealing with Android’s messy file permissions and paths.