cannot get folderId that i just created on google drive

The question:

public class Helpers  extends Activity implements
    GoogleApiClient.OnConnectionFailedListener {
SharedPreferences preferences;

private static final String TAG = "BaseDriveActivity";

public static  String EXISTING_FOLDER_ID;

protected static final int REQUEST_CODE_RESOLUTION = 1;

protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;

public static final String folderId = "FOLDER_ID";
public static final String fileName = "folderId";

private GoogleApiClient mGoogleApiClient;

protected void onResume() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample

protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {

protected void onPause() {
    if (mGoogleApiClient != null) {

final ResultCallback<DriveFolder.DriveFolderResult> callback = new ResultCallback<DriveFolder.DriveFolderResult>() {
    public void onResult(DriveFolder.DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create the folder");

        EXISTING_FOLDER_ID = result.getDriveFolder().getDriveId().toString();
        showMessage("Created a folder: " + result.getDriveFolder().getDriveId().toString());

        preferences = getSharedPreferences(fileName , MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(folderId , EXISTING_FOLDER_ID );

public  void mcreateFolder() {

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
            getGoogleApiClient(), changeSet).setResultCallback(callback);


public void onConnected(Bundle bundle) {
    Log.i(TAG, "GoogleApiClient connected");
    preferences = getSharedPreferences(fileName , MODE_PRIVATE);
    EXISTING_FOLDER_ID = preferences.getString(folderId ,null);
    if (EXISTING_FOLDER_ID == null ) {



public void onConnectionSuspended(int cause) {
    Log.i(TAG, "GoogleApiClient connection suspended");

public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);

public void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();

public GoogleApiClient getGoogleApiClient() {
    return mGoogleApiClient;

I want to create a folder when app is installed and than get its ID and save it to sharedprefrences..after that i wanted to create a file inside that folder but when i use pass that folderID to create file inside it than it says
“Cannot find DriveId. Are you authorized to view this file?”

if I use result.getDriveFolder().getDriveId().getResourceId()
to get id than its always null.
I am using this code to create folder first which I pasted

and to create a file inside a folder I am using this guide
I am passing the folderiD that I save and passing it to create file function.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Do not mix DriveId with ResourceId. Both look like strings, but DriveId is different from ResourceId. See SO 21800257. Also, ResourceId is not immediately available, see SO 22874657.

DriveId usually looks like:


whereas ResourceId is more like:



Since so many developers fight this issue, I’ll try to elaborate as deep as my knowledge allows me to.

     Google Drive             Google Play Svcs      YourApp
   (up in the cloud)         (on your device)      (on your device)
  +--------------------+      +--------------+     +--------------+         
  | 'id' (RESTful API) | - - -> ResourceId  - - - -> ResourceId   |
  +--------------------+      |  DriveId    - - - -> DriveId      |
                              +--------------+     +--------------+

What I’m trying to convey with the artistic expression above is:

  • When you create a drive object (folder/file) on your device, GooPlaySvcs will give you the DriveId
  • You can use this DriveId for local communication with GooPlaySvcs, you can cache it, etc.
  • Per Daniel’s comment in SO 21800257 (link above), do not rely on DriveId to be a constant string,
    it supposedly changes upon the object being committed. Use DriveId.equals() (I did not test that)
  • Anytime you step outside of the local device (Drive web interface, other apps, YourApp on a different
    device), you need to use ResourceId, which is the only unique ID on the Drive (up in the cloud :-).
  • The ResourceId is available to your AFTER GooPlaySvcs commit the object to the Drive. There are
    ways to force it, but it is a different story (search for requestSync()).
  • If you decide to grab the ResourceId and use it for RESTfull calls (delete/trash), be aware
    of the fact that Google Play Services propagates its changes on a schedule you don’t have control
    over (so it seems, see the requestSync() issue again), and your REST/GDAA fight can cause damage
    to your data. GDAA(GooPlayServices) may not be aware of your REST changes for a while. You have
    to manage the synchronization yourself. I admit I failed miserably when I tried.

    Good Luck

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment