Cannot get the folderId I just created in Google Drive

public class Helpers extends Activity implements GoogleApiClient.ConnectionCallbacks, 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; @Override protected void onResume() { super.onResume(); if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } mGoogleApiClient.connect(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) { mGoogleApiClient.connect(); } } @Override protected void onPause() { if (mGoogleApiClient != null) { mGoogleApiClient.disconnect(); } super.onPause(); } final ResultCallback<DriveFolder.DriveFolderResult> callback = new ResultCallback<DriveFolder.DriveFolderResult>() { @Override public void onResult(DriveFolder.DriveFolderResult result) { if (!result.getStatus().isSuccess()) { showMessage("Error while trying to create the folder"); return; } 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 ); editor.apply(); } }; public void mcreateFolder() { MetadataChangeSet changeSet = new MetadataChangeSet.Builder() .setTitle("SmsSync").build(); Drive.DriveApi.getRootFolder(getGoogleApiClient()).createFolder( getGoogleApiClient(), changeSet).setResultCallback(callback); } @Override 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 ) { mcreateFolder(); } } @Override public void onConnectionSuspended(int cause) { Log.i(TAG, "GoogleApiClient connection suspended"); } @Override 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(); return; } 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 the application is installed, and then get its identifier and save it in sharedprefrences..after I wanted to create a file inside this folder, but when I use pass thatIDIDID to create a file inside it, it says "Cannot find DriveId. Are you allowed to view this file? "

if I use result.getDriveFolder().getDriveId().getResourceId() to get id than its always null.
I use this code to create a folder into which I inserted https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFolderActivity.java

and create a file inside the folder. I am using this guide. https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFileInFolderActivity.java
I pass folderiD which I save and pass to create a file function.

0
android google-play-services google-drive-sdk google-drive-android-api
source share
1 answer

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

DriveId usually looks like this:

"DriveId: CAESHDBCMW1RVblahblahahlahblahMYjAUgssy8yYFRTTNKRU55"

whereas ResourceId is more like:

"UW2ablahblaghblahNy00Ums0B1mQ"

UPDATE:

Since many developers are struggling with this problem, I will try to work as deep as it allows me.

  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 using the artistic expression above is:

  • When you create a disk object (folder / file) on your device, GooPlaySvcs will provide you with DriveId
  • You can use this DriveId for local communication with GooPlaySvcs, you can cache it, etc.
  • Per Daniel user comment on SO 21800257 (link above), do not rely on DriveId as a constant line, it supposedly changes to an object that is being executed. Use DriveId.equals () (I have not tested this)
  • At any time, when you go beyond the local device (Drive web interface, other applications, YourApp on another device), you need to use the ResourceId, which is the only unique identifier on the Disk (above in the cloud: -).
  • ResourceId is available for your AFTER. GoOPlaySvcs transfers the object to disk. There are ways to force it, but this is a completely different story (searching for a Sync () request).
  • If you decide to capture the ResourceId and use it for RESTfull calls ( delete / trash ), keep in mind the fact that Google Play services distribute their changes on a schedule that you do not control (so it seems to see requestSync () again ), and your REST / GDAA fight can damage your data. GDAA (GooPlayServices) may not be aware of your REST changes for a while. You have to manage the synchronization yourself. I admit that I failed when I tried.

    Luck

+8
source share

All Articles