Android.Gms.Drive 下载文件帮助

Android.Gms.Drive Download File Assistance

我正在 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);

其中returns所有数据正确。

如有任何帮助,我们将不胜感激。提前谢谢你

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

        IDriveFile file = DriveClass.DriveApi.GetFile(GoogleInfo.GetInstance().SignInApi, driveID);
        file.GetMetadata(mGoogleApiClient).SetResultCallback(metadataRetrievedCallback());
        Task.Run(() =>
        {

            var driveContentsResult = file.Open(mGoogleApiClient,
                DriveFile.ModeReadOnly, null).Await();

            IDriveContents driveContents = driveContentsResult.JavaCast<IDriveApiDriveContentsResult>().DriveContents;

            Stream inputstream = driveContents.InputStream;

            byte[] buffer = new byte[16 * 1024];
            int read;
            MemoryStream output = new MemoryStream();

            while ((read = inputstream.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }