Android Open Source - android-demos Retrieve Contents Activity






From Project

Back to project page android-demos.

License

The source code is released under:

Apache License

If you think the Android project android-demos listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2013 Google Inc. All Rights Reserved.
 */* w  w  w  .j  a v  a2  s. c o m*/
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gms.drive.sample.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveId;

/**
 * Activity to illustrate how to retrieve and read file contents.
 */
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);
        }
    }
}




Java Source Code List

com.google.android.gms.drive.sample.demo.ApiClientAsyncTask.java
com.google.android.gms.drive.sample.demo.BaseDemoActivity.java
com.google.android.gms.drive.sample.demo.CreateEmptyFileActivity.java
com.google.android.gms.drive.sample.demo.CreateFileActivity.java
com.google.android.gms.drive.sample.demo.CreateFileInAppFolderActivity.java
com.google.android.gms.drive.sample.demo.CreateFileInFolderActivity.java
com.google.android.gms.drive.sample.demo.CreateFileWithCreatorActivity.java
com.google.android.gms.drive.sample.demo.CreateFolderActivity.java
com.google.android.gms.drive.sample.demo.CreateFolderInFolderActivity.java
com.google.android.gms.drive.sample.demo.DeleteCustomPropertyActivity.java
com.google.android.gms.drive.sample.demo.EditContentsActivity.java
com.google.android.gms.drive.sample.demo.EditMetadataActivity.java
com.google.android.gms.drive.sample.demo.HomeActivity.java
com.google.android.gms.drive.sample.demo.InsertUpdateCustomPropertyActivity.java
com.google.android.gms.drive.sample.demo.ListFilesActivity.java
com.google.android.gms.drive.sample.demo.ListFilesInFolderActivity.java
com.google.android.gms.drive.sample.demo.PickFileWithOpenerActivity.java
com.google.android.gms.drive.sample.demo.PickFolderWithOpenerActivity.java
com.google.android.gms.drive.sample.demo.PinFileActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesInFolderActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesSharedWithMeActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesWithAInTitleActivity.java
com.google.android.gms.drive.sample.demo.QueryFilesWithCustomPropertyActivity.java
com.google.android.gms.drive.sample.demo.QueryNonTextFilesActivity.java
com.google.android.gms.drive.sample.demo.QuerySortedFilesActivity.java
com.google.android.gms.drive.sample.demo.QueryStarredTextFilesActivity.java
com.google.android.gms.drive.sample.demo.QueryTextOrHtmlFilesActivity.java
com.google.android.gms.drive.sample.demo.ResultsAdapter.java
com.google.android.gms.drive.sample.demo.RetrieveContentsActivity.java
com.google.android.gms.drive.sample.demo.RetrieveContentsWithProgressDialogActivity.java
com.google.android.gms.drive.sample.demo.RetrieveMetadataActivity.java
com.google.android.gms.drive.sample.demo.SyncRequestsActivity.java
com.google.android.gms.drive.sample.demo.events.ListenChangeEventsForFilesActivity.java