Error while reading from the file Google Drive Android

97 views Asked by At

I am trying to fetch the content of a file from My Google Drive but every time I try to fetch the data it is giving me exception Invalid Mode Provided

I have used all the modes available but no luck on that below is my code for fetching content from my file

@Override
    public void onConnected(@Nullable Bundle bundle) {

        Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(driveContentsCallback);
    }


    final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {
        @Override
        public void onResult(DriveApi.DriveContentsResult result) {

            if (result.getStatus().isSuccess()) {
                OpenFileFromGoogleDrive();
            }
        }
    };

    public void OpenFileFromGoogleDrive() {

        String EXISTING_FILE_ID = "1Zv5B9Fhh78bf7vavB1R_x-w3rbjezh28HPfjzjHVQww"; //My File ID
        Drive.DriveApi.fetchDriveId(mGoogleApiClient, EXISTING_FILE_ID).setResultCallback(idCallback);
    }


    final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
        @Override
        public void onResult(DriveApi.DriveIdResult result) {
            if (!result.getStatus().isSuccess()) {
                System.out.println("Cannot find DriveId. Are you authorized to view this file?");
                return;
            }

            DriveId driveId = result.getDriveId();
            DriveFile file = driveId.asDriveFile();
            new RetrieveDriveFileContentsAsyncTask(mGoogleApiClient).execute(file);

        }
    };


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

        RetrieveDriveFileContentsAsyncTask(GoogleApiClient mGoogleApiClient) {
            super(mGoogleApiClient);
        }

        @Override
        protected String doInBackgroundConnected(DriveFile... params) {
            String contents = null;
            DriveFile file = params[0];
            DriveApi.DriveContentsResult driveContentsResult = file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();  `// Exception here`
            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("DRIVE DATA", "IOException while reading from the stream", e);
            }

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

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (result == null) { **// Then resulting in null here**
                System.out.println("Error while reading from the file");
                return;
            }
            System.out.println("File contents: " + result);
        }
    }

ApiClientAsyncTask

    public abstract class ApiClientAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

    private GoogleApiClient mClient;

    public ApiClientAsyncTask(GoogleApiClient mClient) {

        this.mClient = mClient;
    }

    @Override
    protected final Result doInBackground(Params... params) {
        Log.d("TAG", "in background");
        final CountDownLatch latch = new CountDownLatch(1);
        mClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
            @Override
            public void onConnectionSuspended(int cause) {
            }

            @Override
            public void onConnected(Bundle arg0) {
                latch.countDown();
            }
        });
        mClient.registerConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(ConnectionResult arg0) {
                latch.countDown();
            }
        });
        mClient.connect();
        try {
            latch.await();
        } catch (InterruptedException e) {
            return null;
        }
        if (!mClient.isConnected()) {
            return null;
        }
        try {
            return doInBackgroundConnected(params);
        } finally {
            mClient.disconnect();
        }
    }

    protected abstract Result doInBackgroundConnected(Params... params);

    protected GoogleApiClient getGoogleApiClient() {
        return mClient;
    }
}

The EXISTING_FILE_ID I am using is correct as I am using othere method to get the file name and it is displaying the file name correctly.

Any help is appreciable. Thanks

0

There are 0 answers