Search code examples
androidgoogle-drive-apidelete-filegoogle-drive-android-api

Nullpointer in delete file from gdrive


First error come when I'm trying to get Resource id from Driveid.

DriveFile dfile= Drive.DriveApi.getFile(mGoogleApiClient,DriveId.decodeFromString(driveId));                                            
Log.e(TAG,"Driveid>>>>" + driveId);                                             
String resourceID= dfile.getDriveId().getResourceId().toString();

Whenever I got Resource id and trying to delete item from google drive.

com.google.api.services.drive.Drive service;

service.files().delete(resourceID).execute();

Here Logcat ERROR:

enter image description here

Please Give me standard Solution for delete file from google drive.


Solution

  • For 'trash', there is no need to mix the GDAA with the REST Api anymore. Since GooPlaySvcs release 7.0 (March 2015), there is a 'trash()' method in the GDAA that does not require the ResourceId, shielding you from the timing issues related to the the latency/existence of it.

    For short demonstration, here is a 'trash' wrapper for the GDAA that does not need ResourceId. On top of it, you don't need to worry about the network (wifi) on-line / off-line state.

    private static GoogleApiClient mGAC;
    ...  
    static void trash(DriveId dId) {
      if (mGAC != null && mGAC.isConnected() && dId != null) {
        DriveResource driveResource;
        if (dId.getResourceType() == DriveId.RESOURCE_TYPE_FOLDER) {
          driveResource = Drive.DriveApi.getFolder(mGAC, dId);
        } else {
          driveResource = Drive.DriveApi.getFile(mGAC, dId);
        }
        if (driveResource != null) {
          driveResource.trash(mGAC).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
              // bingo, trashed successfully !!!   
            }
          });
        }
      }
    }
    

    Good Luck