我正在尝试实现一个Android应用,其中该应用将图像上传到用户google驱动器。我成功实现了here给出的快速入门指南。它正在上传图片,但使用android Activity上传图片的所有示例/示例。我经历了Google提供的documentation,但它的耦合程度很低。
是否有任何示例/示例在不使用活动的情况下上传文件,以便以后可以将其转换为后台服务?
我达到了以下几点。现在,此时GoogleSignIn.getLastSignedInAccount
返回null,我正在寻找一种解决方案,它选择帐户并获得用户的许可。在google示例中,它似乎在startActivityForResult中执行。我也尝试过寻找它,但没有适用于Android的Drive REST v3示例。
package com.mpathak.drivetesting;
import android.content.Context;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.drive.CreateFileActivityOptions;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveClient;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveResourceClient;
import com.google.android.gms.drive.MetadataChangeSet;
import com.google.android.gms.tasks.Task;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class DriveSample {
private GoogleSignInClient mGoogleSignInClient;
private DriveClient mDriveClient;
private DriveResourceClient mDriveResourceClient;
private Context _context;
private static final String TAG = "drive-Testing";
public DriveSample(Context context)
{
_context = context;
}
public void uploadFile()
{
GoogleSignInClient GoogleSignInClient = buildGoogleSignInClient();
mDriveClient = Drive.getDriveClient(_context, GoogleSignIn.getLastSignedInAccount(_context));
mDriveResourceClient =
Drive.getDriveResourceClient(_context, GoogleSignIn.getLastSignedInAccount(_context));
saveFileToDrive();
}
private GoogleSignInClient buildGoogleSignInClient() {
GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build();
return GoogleSignIn.getClient(_context, signInOptions);
}
private void saveFileToDrive() {
Log.i(TAG, "Creating new contents.");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
final Bitmap image = BitmapFactory.decodeFile("/DCIM/Camera/some_image.jpg", options);
mDriveResourceClient
.createContents()
.continueWithTask(
task -> createFileIntentSender(task.getResult(), image))
.addOnFailureListener(
e -> Log.w(TAG, "Failed to create new contents.", e));
}
private Task<Void> createFileIntentSender(DriveContents driveContents, Bitmap image) {
Log.i(TAG, "New contents created.");
OutputStream outputStream = driveContents.getOutputStream();
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e) {
Log.w(TAG, "Unable to write file contents.", e);
}
MetadataChangeSet metadataChangeSet =
new MetadataChangeSet.Builder()
.setMimeType("image/jpeg")
.setTitle("Android Photo.png")
.build();
CreateFileActivityOptions createFileActivityOptions =
new CreateFileActivityOptions.Builder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(driveContents)
.build();
return mDriveClient
.newCreateFileActivityIntentSender(createFileActivityOptions)
.continueWith(
task -> {
return null;
});
}
}