4

Hai am following the Google Drive Api Demos. Here am not able to create a file inside the public folder

package com.google.android.gms.drive.sample.demo;
import android.os.Bundle;

import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.ContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveFolder.DriveFileResult;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataChangeSet;

import java.lang.Override;


/**
 * An activity to create a file inside a folder.
*/
public class CreateFileInFolderActivity extends BaseDemoActivity {

private DriveId mFolderDriveId;

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FOLDER_ID)
            .setResultCallback(idCallback);
}

final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
    @Override
    public void onResult(DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Cannot find DriveId. Are you authorized to view this file?");
            return;
        }
        mFolderDriveId = result.getDriveId();
        Drive.DriveApi.newContents(getGoogleApiClient())
                .setResultCallback(contentsResult);
    }
};

final private ResultCallback<ContentsResult> contentsResult = new
        ResultCallback<ContentsResult>() {
    @Override
    public void onResult(ContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }
        DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), mFolderDriveId);
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle("Sujatha.txt")
                .setMimeType("text/plain")
                .setStarred(true).build();
        folder.createFile(getGoogleApiClient(), changeSet, result.getContents())
                .setResultCallback(fileCallback);
    }
};

final private ResultCallback<DriveFileResult> fileCallback = new
        ResultCallback<DriveFileResult>() {
    @Override
    public void onResult(DriveFileResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create the file");
            return;
        }
        showMessage("Created a file: " + result.getDriveFile().getDriveId());
    }
};

}

my public folder is this and also
EXISTING_FOLDER_ID = "0B2uTE9KZI1PTRXEwQ00xRkt0Wk0"

still am not able to create a text file in this folder. Its showing this message Cannot find DriveId. Are you authorized to view this file? I made the folder public and its set to can edit in share options.

Any suggestions will be appreciated.

Thanks in advance

EDIT 1

  1. Am able to create files and folder in my google drive. But not to a specific folder which I mentioned above.
suja
  • 1,198
  • 2
  • 12
  • 32
  • Have you registered your app with Google Developers Console? – Manish Mulimani May 02 '14 at 06:14
  • yes i have registered it.. am able to create file inside my gdrive.. but not able to create file within this folder... – suja May 02 '14 at 06:15
  • May be this answer will help you http://stackoverflow.com/questions/37385591/how-to-upload-a-file-to-google-drive-folder-using-android-google-drive-sdk/37390212#37390212 – Bikesh M May 23 '16 at 11:54

2 Answers2

0

Create a file from your end and upload that to GDrive folder

private void saveFileToDrive(final String name) {
        Thread t = new Thread(new Runnable() {
          @Override
          public void run() {
              try
              {
                  FileContent mediaContent = new FileContent("plain/text", yourfilename);
                  com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
                  body.setTitle(name);
                  body.setMimeType("plain/text"); 
                  com.google.api.services.drive.model.File file = servicer.files().insert(body, mediaContent).execute();
                  if (file != null)
                  {
                      runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                        Toast toast = Toast.makeText(getApplicationContext(), "File exported successfully", 2000).toast.show();
                        }
                      }
                      );

                  }

              }
              catch (UserRecoverableAuthIOException e)
              {
                  startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
          }
      });
      t.start();
    }
Sreedhu Madhu
  • 2,480
  • 2
  • 30
  • 40
  • thanks for your reply.. I can create file and folder in gdrive but I want to create file in a specific folder mentioned in my question. I have edited my question. – suja May 02 '14 at 07:13
0

Your app is not granted access to that folder unless the user of your app selects that folder from within your app. Read it more here - https://stackoverflow.com/a/25881227/1363471

Community
  • 1
  • 1
nightlytrails
  • 2,448
  • 3
  • 22
  • 33