Android.Gms.Drive下载文件协助

时间:2018-09-15 11:42:11

标签: c# android google-api xamarin.android google-drive-android-api

我正在android上创建一个应用程序(可能会扩展到IOS,但首先关注android),该功能会将用户为该应用程序保存的数据上传到Google云端硬盘,也可以下载。

我正在使用Android.Gms.Drive api来实现此目标,这是Google开发人员页面建议的。我已经到了用户可以登录和注销以及上载已保存文件的地步,但是我不知道如何下载文件。

我可以找到要下载的文件的元数据,但不确定如何使用它来打开文件。

这是我用来连接的代码,在一些示例中被拼凑在一起,所以我不确定我做对了吗

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);

正确返回所有数据。

任何帮助将不胜感激。预先谢谢你

1 个答案:

答案 0 :(得分:0)

没关系,我找到了解决方案。

    Object s1 = 5;
    Object s2 = new Integer(5);
    Object s3 = 5;
    System.out.println("identityHashCode : " + System.identityHashCode(s1) + " HashCode : " + s1.hashCode());
    System.out.println("identityHashCode : " + System.identityHashCode(s2) + " HashCode : " + s2.hashCode());
    System.out.println("identityHashCode : " + System.identityHashCode(s3) + " HashCode : " + s3.hashCode());

    //Output
    identityHashCode : 2018699554 HashCode : 5
    identityHashCode : 1311053135 HashCode : 5
    identityHashCode : 2018699554 HashCode : 5