bimal chawla
bimal chawla

Reputation: 449

How to delete file from Google Drive

I am using android-demos for implementing Google Drive integration in android. I successfully created a file in google drive. Now i want to delete that newly created file. I found the reference for this through https://developers.google.com/drive/v2/reference/files/delete Now files() method in this link is not found in Drive.

private static void deleteFile(Drive service, String fileId) { try { service.files().delete(fileId).execute(); } catch (IOException e) { System.out.println("An error occurred: " + e); } }

Now please tell me how to delete file from Google Drive. I did research on this but found no solution. Some says use previous api of Google Drive. But that is obsolete now. Now Goole uses V2 for Drive.

Upvotes: 1

Views: 3102

Answers (3)

Sunil Chaudhary
Sunil Chaudhary

Reputation: 1247

I have done it successfull **

private GoogleApiClient api;

**

   public void Update(DriveId dId) {
    try {
        DriveFile sumFile = dId.asDriveFile();
        com.google.android.gms.common.api.Status deleteStatus =
                sumFile.delete(api).await();
        if (!deleteStatus.isSuccess()) {
            Log.e(TAG, "Unable to delete app data.");

        } else {
            // Remove stored DriveId.
            preferences_driverId.edit().remove("drive_id").apply();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Amol Suryawanshi
Amol Suryawanshi

Reputation: 2184

Deleting files from Google drive using core API is not yet supported. So you must use Restful API calls. To do restful API calls you need to add following jars to your lib folder

google-api-client-1.19.1.jar
google-api-client-android-1.19.1.jar
google-api-services-drive-v2-rev158-1.19.1.jar
google-http-client-1.19.0.jar
google-http-client-android-1.19.0.jar
google-http-client-gson-1.19.0.jar
google-oauth-client-1.19.0.jar
gson-2.1.jar
jsr305-1.3.9.jar

now you can do restful API call withing core API calls as follows

    com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential crd = GoogleAccountCredential
                .usingOAuth2(
                        ctx,
                        Arrays.asList(com.google.api.services.drive.DriveScopes.DRIVE_FILE));
        crd.setSelectedAccountName(email);
        _drvSvc = new com.google.api.services.drive.Drive.Builder(
                AndroidHttp.newCompatibleTransport(), new GsonFactory(), crd).setApplicationName("SmsAndCallLogBackup")
                .build();

remember i connected to Google drive using core API. and for deleting only I am using restful API

following method is used to delete file from Google drive

public void delete(DriveId dId) {
        try {
            String fileID = dId.getResourceId();
            if (fileID != null)
                _drvSvc.files().delete(fileID).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

call this method in async task otherwise it gives error it will work definitely

Upvotes: 0

pinoyyid
pinoyyid

Reputation: 22286

Last time I checked, there is no delete in GDAA. See How to delete a file on google drive using Google Drive Android API

You can either wait for it to be implemented, or use the REST API https://developers.google.com/drive/v2/reference/files/delete

I suspect you are confusing two different APIs. GDAA is a purely local API, ie. your app is communicating with the Android Drive app. With the REST API, your app is talking over http to the Google Drive servers. Your app can use either, or a mixture of both (although you need to be pretty desparate to do that).

Upvotes: 3

Related Questions