我正在编写一个可以直接从 Google Drive 播放媒体的应用程序。
谷歌文档提到
https://www.googleapis.com/auth/drive.readonly
范围(请参阅此处:https : //developers.google.com/drive/web/scopes)这似乎正是我所需要的。然而,Android 驱动器 API 似乎只支持那里提到的两个范围:
Drive.SCOPE_FILE Drive.SCOPE_APPFOLDER
所以我尝试了几个像这样的非标准范围:
mGoogleApiClient = new GoogleApiClient.Builder(XenoAmp.getContext())
.addApi(Drive.API)
//.addApi(DriveScopes.)
//.addScope(Drive.SCOPE_APPFOLDER)
.addScope(Drive.SCOPE_FILE)
.addScope(new Scope("https://www.googleapis.com/auth/drive"))
//.addScope(new Scope("https://www.googleapis.com/auth/drive.readonly"))
//.addScope(new Scope("https://www.googleapis.com/auth/drive.metadata.readonly"))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
Run Code Online (Sandbox Code Playgroud)
然而我的 logcat 抱怨:
03-29 12:53:58.935:E/ClientConnectionOperation(9953):com.google.android.gms.drive.auth.c:授权失败:不支持的范围:https : //www.googleapis.com/auth/drive
那么 - 驱动范围不再受支持吗?那么如何才能访问 Goodle Drive 上的所有用户媒体文件?
我想使用Google Drive 作为 服务器来存储应用程序数据。因此,我已将所有数据写入一个文件,然后将该文件上传到云端硬盘。
代码:
try {
// outputStream.write(bitmapStream.toByteArray());
outputStream.write(text.getBytes());
}
catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("text/txt").setTitle("testfile.txt").build();
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
}
catch (SendIntentException e) {
Log.i(TAG, "Failed to launch file chooser.");
}
Run Code Online (Sandbox Code Playgroud)
是否有可能每当设备连接到互联网时,应用程序数据就会与 Google Drive 上的可用数据同步?我读到了Google Drive API,但无法理解:
1)同步将如何发生?2)我们可以与Google Drive的App文件夹中存储的文件同步吗?3)我是否需要将文件写入Google Drive,或者我必须将文件保存到某个容器中,并且Google会在连接到互联网时自行更新(如在ios中)?
请指导我。
编辑
在驱动器中创建文件之前,我已经做了检查。
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.MIME_TYPE, …Run Code Online (Sandbox Code Playgroud) 我刚刚使用Udacity 上的课程、开发人员文档和GitHub 上的示例作为资源在我的应用程序中实现了 Google 登录。
我选择在我的应用程序中添加登录的原因是,用户可以在他们登录到该帐户的任何设备上访问他们的数据 - 即数据将保存在云上,可以这么说。
但是,我在开发人员文档或 Udacity 课程中找不到任何关于如何使用 Google 登录将数据保存到云的内容。
我查看了Stack Overflow 上类似问题的答案,该问题建议使用Google Drive Android API来执行此操作。
我还查看了 Firebase中的数据库,这对我来说是全新的。
所有这些让我相信我需要使用我自己的服务器来存储用户的数据(例如 Firebase,我认为,这是一项付费服务),或者我应该使用 Google Drive API。
所以,我的问题是,是否有特定的 API 可以使用用户的 Google 登录将数据保存到云中,还是我必须使用自己的服务器或 Google Drive API?
编辑:
为了澄清评论中提出的一些观点,如果我想要超过 100 个并发数据库连接(我现在不需要,但很快我可能会有 100 多个用户同时保存数据),我假设 Firebase 是其定价页面上的付费服务)。
我知道 Firebase 有自己的身份验证系统,但我没有考虑使用 Firebase,直到我意识到我需要某个地方来保存数据,这就是为什么我没有从一开始就开始使用它。
我知道 Firebase 与 Google Drive 不同,但我想知道哪个最适合我的目的。我的应用程序当前读取/写入数据到设备上的 SQLite 数据库。我想根据用户的 Google 帐户保存这些数据,以便他们可以在不同的设备上登录并拥有相同的数据。
android google-drive-android-api firebase-realtime-database google-signin
当照片上传到 Google 云端硬盘或 Google 照片并分享给其他人时,链接将为 \xe2\x80\x8b https://docs.google.com/xyz。但是,有没有办法将其转换为有意义的链接,例如“Presentation 2017 链接”?
\n这可能是一个愚蠢的问题,但我找不到任何解决方案。我需要知道如何备份 roomdatabase 并使用 Google Drive 检索它。那么房间数据库的mime类型是什么?谁能知道如何做到这一点?让我发布我迄今为止尝试过的内容:
private void saveFileToDrive(File file) {
mDriveClient = Drive.getDriveClient(this, GoogleSignIn.getLastSignedInAccount(this));
// Build a drive resource client.
mDriveResourceClient =
Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this));
final Task<DriveFolder> rootFolderTask =mDriveResourceClient.getRootFolder();
final Task<DriveContents> createContentsTask =mDriveResourceClient.createContents();
Tasks.whenAll(rootFolderTask, createContentsTask)
.continueWithTask(new Continuation<Void, Task<DriveFile>>() {
@Override
public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
DriveFolder parent = rootFolderTask.getResult();
DriveContents contents = createContentsTask.getResult();
// OutputStream outputStream = contents.getOutputStream();
// try (Writer writer = new OutputStreamWriter(outputStream)) {
// writer.write("Hello World!");
// }
OutputStream oos = contents.getOutputStream();
if (oos …Run Code Online (Sandbox Code Playgroud) 我正在开发一个使用 Google Drive REST API 的 android 应用程序。我只想列出驱动器中的所有文件。但是当该方法listDriveFiles()被调用时,我得到一个错误为java.lang.IllegalArgumentException: the name must not be empty: null.
我发现这里提出了类似的问题,其中大多数都说要添加权限GET_ACCOUNT。我已经这样做了,但没有运气。一个答案指向检查 的值,getAccount()当我这样做时,我得到的值为null。
错误:
java.lang.IllegalArgumentException: the name must not be empty: null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5388)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: the name must not be empty: null
at android.accounts.Account.<init>(Account.java:48)
at com.google.android.gms.auth.zzd.getToken(Unknown Source)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) …Run Code Online (Sandbox Code Playgroud) 我正在构建一个应用程序,我将应用程序数据存储在 Google Drive 上的应用程序特定文件夹中。我已经能够设置与文件存储和检索相关的所有内容。我面临的问题是关于权限。用户可以选择从 Google Drive 设置面板断开应用程序。
我使用DriveScopes.DRIVE_APPDATA含义https://www.googleapis.com/auth/drive.appdata范围来保存数据。
我试图弄清楚如何找出这是否发生在应用程序端。如果我尝试继续使用与驱动器相关的 api,应用程序被断开连接,那么它会崩溃并显示UserRecoverableAuthException.
com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:297)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:476)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:409)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:526)
at abhiank.maplocs.ui.drivesync.DriveSyncService.onHandleIntent(DriveSyncService.kt:68)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:78)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
at com.google.android.gms.auth.zze.zzb(Unknown Source:13)
at com.google.android.gms.auth.zzd.zza(Unknown Source:77)
at com.google.android.gms.auth.zzd.zzb(Unknown Source:20)
at com.google.android.gms.auth.zzd.getToken(Unknown Source:7)
at com.google.android.gms.auth.zzd.getToken(Unknown Source:5)
at com.google.android.gms.auth.zzd.getToken(Unknown Source:2)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source:55)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:267)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:292)
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法来确定应用程序是否没有权限或范围。
GoogleSignInAccount从GoogleSignIn.getLastSignedInAccount(this). account.grantedScopes()在应用程序断开连接后,它具有以下可用范围。drive.appdata即使应用程序已断开连接也会显示。[https://www.googleapis.com/auth/drive.appdata, https://www.googleapis.com/auth/userinfo.profile, https://www.googleapis.com/auth/userinfo.email, openid, …Run Code Online (Sandbox Code Playgroud) 编辑:我已将文本设置为如下字符串:
String text =("Hello!");
我想将其转换为纯文本文件,然后上传到Google云端硬盘文件夹.我试过下面的代码,但它没有完整,所以我不能说出现了什么错误.
我正在使用谷歌硬盘"快速启动"演示,并尝试根据我的需要定制它.链接:https://github.com/googledrive/android-quickstart
DriverClass:
public class UploadDrive extends Activity implements ConnectionCallbacks,OnConnectionFailedListener {
private static final String TAG = "androiddrivequickstart";
private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
private static final int REQUEST_CODE_CREATOR = 2;
private static final int REQUEST_CODE_RESOLUTION = 3;
private GoogleApiClient mGoogleApiClient;
private Bitmap mBitmapToSave;
private void saveFileToDrive() {
// Start by creating a new contents, and setting a callback.
Log.i(TAG, "Creating new contents.");
//How to call? Can i use File from java.io? …Run Code Online (Sandbox Code Playgroud) 我使用新的谷歌驱动器API,我无法从我的谷歌驱动器获取所有文件夹,我只获得我用谷歌驱动器api创建的文件夹...任何人都知道为什么会发生这种情况?
这是我的代码:
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client. Once connected, the camera is launched.
mGoogleApiClient.connect();
}
/**
* Handles resolution callbacks.
*/ …Run Code Online (Sandbox Code Playgroud) 我想从我的应用程序上传一个db文件到谷歌驱动器.我能够在谷歌驱动器中创建一个文件夹,但我没有得到如何上传数据库文件.
这是我的代码.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveFolder.DriveFolderResult;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.Metadata;
import com.google.android.gms.drive.MetadataChangeSet;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.api.client.http.FileContent;
//import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
//import com.google.api.services.drive.Drive;
public class MainActivity extends BaseDemoActivity {
private final static String TAG = "MainActivity";
DriveId …Run Code Online (Sandbox Code Playgroud)