简体   繁体   English

我无法使用Google Drive下载文件的内容

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

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}. 我正在尝试使用Google Drive下载Google Doc文件,但我不断收到状态消息:Status {statusCode =此文件无内容。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. 这是处理Google驱动器的类。

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 . 我已使用此资源来帮助我https://developers.google.com/drive/android/files

As far as I can discern, the Google Drive Android API does not provide access to Google Docs native content, eg Docs and Spreadsheets. 据我所知,Google Drive Android API不提供对Google Docs本机内容(例如Docs和Spreadsheets)的访问。 Only plain blob files stored in Drive. 仅普通的Blob文件存储在云端硬盘中。

I would love to be proven wrong, though. 不过,我很想证明自己是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM