1

I am new in Google Drive Integration in Android Application.I need to know whether its possible to read all the contents of google drive from our customize android app.

For Eg: I need to open an excel file which is stored in google drive & need to fetch the number of rows and columns.

I have seen the below link:

Android google drive demos

But I got confused with the below lines:

com.google.android.gms.drive.sample.demo.BaseDemoActivity.EXISTING_FOLDER_ID
com.google.android.gms.drive.sample.demo.BaseDemoActivity.EXISTING_FILE_ID

I have seen below link Android Google Drive EXISTING_FOLDER_ID,EXISTING_FILE_ID

But no Hope

What am i supposed to add as Drive ID.Its shows null always.

Code:

RetrieveContentsActivity:

public class RetrieveContentsActivity extends BaseDemoActivity {

    private static final String TAG = "RetrieveContentsActivity";

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

    final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
        @Override
        public void onResult(DriveIdResult result) {
            new RetrieveDriveFileContentsAsyncTask(
                    RetrieveContentsActivity.this).execute(result.getDriveId());
        }
    };

    final private class RetrieveDriveFileContentsAsyncTask
            extends ApiClientAsyncTask<DriveId, Boolean, String> {

        public RetrieveDriveFileContentsAsyncTask(Context context) {
            super(context);
        }

        @Override
        protected String doInBackgroundConnected(DriveId... params) {
            String contents = null;
            DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), params[0]);
            DriveContentsResult driveContentsResult =
                    file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
            if (!driveContentsResult.getStatus().isSuccess()) {
                return null;
            }
            DriveContents driveContents = driveContentsResult.getDriveContents();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(driveContents.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
                contents = builder.toString();
            } catch (IOException e) {
                Log.e(TAG, "IOException while reading from the stream", e);
            }

            driveContents.discard(getGoogleApiClient());
            return contents;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (result == null) {
                showMessage("Error while reading from the file");
                return;
            }
            showMessage("File contents: " + result);
        }
    }
}

Kindly help me out how i am supposed to achieve it.Thanks

Community
  • 1
  • 1
  • In my project, `DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), params[0]);` displays error showing unresolved reference: getFile, what should be done? Please hep! – hetsgandhi Aug 03 '18 at 07:26

1 Answers1

0

"For Eg: I need to open an excel file which is stored in google drive & need to fetch the number of rows and columns."

Google Drive stores 'stuff' as blobs, so is unable to parse the blob content into rows and columns.

if you uploaded excel with convert=true, then it will create a Google Spreadhseet, and then you can use the Spreadsheet API to fetch rows and columns.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thanks,Is it possible to fetch any images or any other text file ? Can you please tell how can i get all the files from google drive and list it in my application... –  Feb 26 '15 at 05:26
  • You can fetch any file type. To list them you use the files.list API call. see https://developers.google.com/drive/v2/reference/files/list – pinoyyid Feb 26 '15 at 05:42
  • 1
    Thanks Again but can you please tell me how can i get Drive ID Here RetrieveContentsActivity.this).execute(result.getDriveId()); show null –  Feb 26 '15 at 07:40