I am unable to download contents of a file using google drive

若如初见. 提交于 2019-12-25 04:46:22

问题


I am attempting to download a google doc file with google drive but I keep getting the status message: Status{statusCode=No content is available for this file., resolution=null}. This problem persists for any file I attempt to download. The files that I am downloading are not empty and have text in them. This is the class that handles google drive.

package com.parse.starter;

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

import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
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.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveFile.DownloadProgressListener;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.OpenFileActivityBuilder;

public class WriteScreen extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {

    GoogleApiClient mGoogleApiClient;
    private DriveId mSelectedFileDriveId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_screen);

        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    private ResultCallback<DriveContentsResult> contentsOpenedCallback = new ResultCallback<DriveContentsResult>() {
        @Override
        public void onResult(DriveContentsResult result) {
            if (!result.getStatus().isSuccess()) {
                // display an error saying file can't be opened
                System.out.println("error : " + result.getStatus());
                return;
            }
            // DriveContents object contains pointers
            // to the actual byte stream
            DriveContents contents = result.getDriveContents();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    contents.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String contentsAsString = builder.toString();
        }
    };

    @Override
    protected void onActivityResult(final int requestCode,
            final int resultCode, final Intent data) {
        switch (requestCode) {
        case 5:
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            }
            break;
        case 6:
            if (resultCode == RESULT_OK) {
                mSelectedFileDriveId = (DriveId) data
                        .getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

                DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
                        mSelectedFileDriveId);
                file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY,
                        new DownloadProgressListener() {
                            @Override
                            public void onProgress(long bytesDownloaded,
                                    long bytesExpected) {
                                // Update progress dialog with the latest
                                // progress.
                                int progress = (int) (bytesDownloaded * 100 / bytesExpected);
                                Log.d("Tag", String.format(
                                        "Loading progress: %d percent",
                                        progress));
                            }
                        }).setResultCallback(contentsOpenedCallback);
            }
        default:
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onConnected(Bundle result) {
        System.out.println("Connection passed");
    }

    @Override
    public void onConnectionSuspended(int arg0) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        System.out.println("Connection failed: " + result);
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, 5);
            } catch (IntentSender.SendIntentException e) {
                // Unable to resolve, message user appropriately
            }
        } else {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
        }
    }

    public void open(View view) {
        fileOpen();
    }

    public void publish(View view) {
        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(
                        new String[] { "application/vnd.google-apps.document",
                                "text/plain" }).build(mGoogleApiClient);

        try {
            startIntentSenderForResult(intentSender, 6, null, 0, 0, 0);
        } catch (SendIntentException e) {
            System.out.println("publish failed: " + e);
        }
    }

    private void fileOpen() {
        Intent intent = getPackageManager().getLaunchIntentForPackage(
                "com.google.android.apps.docs.editors.docs");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            startActivity(intent);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.write_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return super.onOptionsItemSelected(item);
    }

}

I have used this resource to help me out https://developers.google.com/drive/android/files.


回答1:


As far as I can discern, the Google Drive Android API does not provide access to Google Docs native content, e.g. Docs and Spreadsheets. Only plain blob files stored in Drive.

I would love to be proven wrong, though.



来源:https://stackoverflow.com/questions/27728614/i-am-unable-to-download-contents-of-a-file-using-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!