[英]I am unable to download contents of a file using google drive
我正在嘗試使用Google Drive下載Google Doc文件,但我不斷收到狀態消息:Status {statusCode =此文件無內容。resolution = null}。 對於我嘗試下載的任何文件,此問題仍然存在。 我正在下載的文件不為空,並且其中包含文本。 這是處理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);
}
}
我已使用此資源來幫助我https://developers.google.com/drive/android/files 。
據我所知,Google Drive Android API不提供對Google Docs本機內容(例如Docs和Spreadsheets)的訪問。 僅普通的Blob文件存儲在雲端硬盤中。
不過,我很想證明自己是錯誤的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.