Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

android - Invalid parent folder error for AppFolder in Drive API

I get an invalid parent folder error, and I've seen the solutions to use resource ID rather than Drive ID, but it's a different scenario here.

I'm trying to access the AppFolder, and this just uses the GoogleApiClient like so:

this.appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient);

When I later try to create a file in it, I get the above error.

DriveFolder.DriveFileResult fileResult = appFolder.createFile(mGoogleApiClient, changeSet, driveContentsResult.getDriveContents()).await();

Then fileResult.getStatus() gives me the erros.

This used to work for me before. What's different is that I've manually emptied my app's data on Google Drive (delete hidden app data). But I haven't disconnected the app - so I would assume that appFolder will continue to work the same way...

So far the only workaround is uninstalling the app, but this way I lose my data.

This is reproducible. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update: This issue, #4483, was fixed in January 2017. The following fix may not apply anymore.

Since this continues to be an outstanding issue, I have taken steps to establish a work-around that can be done with code without resorting to user intervention.

As @seanpj says, this issue does not always occur and seems to be dependent upon the sync status of the Drive service. However, if the problem does occur, the following method works for me to circumvent the problem. I post it here in case it may be helpful to someone else. The Javadoc has more information.

/**
 * Works around the Drive API for Android (GDAA) issue where the appDataFolder becomes
 * unavailable after hidden app data is deleted through the online Drive interface
 * (Settings->Manage Apps->Delete hidden app data) by using the REST v3 API to create an
 * empty file in appDataFolder. The file is immediately deleted after creation but leaves
 * the appDataFolder in working order.
 * See <a href="https://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=4483"
 * target="_blank">apps-api-issues #4483</a>
 * <p>
 * Call this method after a call to the Drive API fails with error code 1502
 * (DriveStatusCodes.DRIVE_RESOURCE_NOT_AVAILABLE) when dealing with the appDataFolder then
 * try the failed operation again.
 * <p>
 * This method assumes that all appropriate permissions have been granted and authorizations
 * made prior to invocation.
 *
 * @param context - Context
 * @param account The account name that has been authorized, e.g., [email protected].
 * @throws IOException From the REST API.
 */
private void fixAppDataFolder(Context context, String account) throws IOException {
    final String[] SCOPES = {DriveScopes.DRIVE_APPDATA};
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
            context, Arrays.asList(SCOPES)).setBackOff(new ExponentialBackOff());
    HttpTransport transport = AndroidHttp.newCompatibleTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    com.google.api.services.drive.Drive driveService;

    credential.setSelectedAccountName(account);
    driveService = new com.google.api.services.drive.Drive.Builder(
            transport, jsonFactory, credential)
            .setApplicationName("Your app name here")
            .build();

    File fileMetadata = new File();
    fileMetadata.setName("fixAppDataFolder")
            .setMimeType("text/plain")
            .setParents(Collections.singletonList("appDataFolder"));

    File appDataFile = driveService.files()
            .create(fileMetadata)
            .setFields("id")
            .execute();

    driveService.files().delete(appDataFile.getId()).execute();
} // fixAppDataFolder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...