【发布时间】:2018-09-15 11:42:10
【问题描述】:
我正在 android 上创建一个应用程序(可能扩展到 IOS,但首先专注于 android),作为一项功能,将用户保存的应用程序数据上传到 Google Drive,也可以下载它。
按照 Google 开发页面的建议,我正在使用 Android.Gms.Drive api 来实现此目的。我已经到了用户可以登录和注销,以及上传保存的文件的地步,但我不知道如何下载文件。
我可以找到我要下载的文件的元数据我不确定如何使用它来打开文件。
这是我用来连接的代码,它是从一些示例拼凑而成的,所以我不确定我是否做得对
namespace TestApp.Droid
{
[Activity(Label = "TestApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
const string TAG = "MainActivity";
const int RC_SIGN_IN = 9001;
GoogleApiClient mGoogleApiClient;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// [START configure_signin]
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
.RequestEmail()
.RequestScopes(new Scope(Constants.scopes))
.RequestScopes(DriveClass.ScopeFile)
.RequestScopes(DriveClass.ScopeAppfolder)
.Build();
// [END configure_signin]
// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.AddApi(Auth.GOOGLE_SIGN_IN_API,gso)
.AddApi(DriveClass.API)
.AddOnConnectionFailedListener(OnConnectionFailed)
.Build();
if (!mGoogleApiClient.IsConnected) {
mGoogleApiClient.Connect(GoogleApiClient.SignInModeOptional);
}
// [END build_client]
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnStart()
{
base.OnStart();
var opr = Auth.GoogleSignInApi.SilentSignIn(mGoogleApiClient);
if (opr.IsDone)
{
var result = opr.Get() as GoogleSignInResult;
HandleSignInResult(result);
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN)
{
var result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
HandleSignInResult(result);
}
}
public void HandleSignInResult(GoogleSignInResult result)
{
if (result.IsSuccess)
{
// Signed in successfully, show authenticated UI.
var acct = result.SignInAccount;
if (!mGoogleApiClient.IsConnected)
{
mGoogleApiClient.Connect(GoogleApiClient.SignInModeOptional);
}
}
else {
GoogleInfo.GetInstance().Result = result.Status.ToString(); ;
}
}
public void SignIn()
{
var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
StartActivityForResult(signInIntent, RC_SIGN_IN);
}
public void SignOut()
{
Auth.GoogleSignInApi.SignOut(mGoogleApiClient);
}
void RevokeAccess()
{
Auth.GoogleSignInApi.RevokeAccess(mGoogleApiClient);
}
protected override void OnStop()
{
base.OnStop();
mGoogleApiClient.Disconnect();
}
}
}
这是我用来获取元数据的
DriveClass.DriveApi.GetRootFolder(mGoogleApiClient).ListChildrenAsync(mGoogleApiClient);
正确返回所有数据。
任何帮助将不胜感激。提前谢谢你
【问题讨论】:
标签: c# android google-api xamarin.android google-drive-android-api