使用Xamarin.Android将文件上传到google drive文件夹

时间:2017-12-03 13:56:48

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

我想使用Xamarin.Andriod

在google驱动器(而不是默认位置)的特定文件夹中创建文件

我使用以下代码

 MetadataChangeSet changeSetfile = new MetadataChangeSet.Builder()
                   .SetTitle("Test.jpg")
                   .SetMimeType("image/jpeg")
                   .Build();

            DriveClass.DriveApi
                   .GetRootFolder(_googleApiClient)
                    .CreateFile(_googleApiClient, changeSetfile, contentResults.DriveContents);

1 个答案:

答案 0 :(得分:0)

  1. 实施GoogleApiClient.IConnectionCallbacks

  2. 使用GoogleApiClientDriveClass.API

  3. 获取DriveClass.ScopeFile

    GoogleApiClient示例:

    if (_googleApiClient == null) //  _googleApiClient is a class level variable
    {
        _googleApiClient = new GoogleApiClient.Builder(this)
          .AddApi(DriveClass.API)
          .AddScope(DriveClass.ScopeFile)
          .AddConnectionCallbacks(this)
          .AddOnConnectionFailedListener(onConnectionFailed)
          .Build();
    }
    if (!_googleApiClient.IsConnected)
        _googleApiClient.Connect();
    
    1. 连接后,查询文件夹,根据需要创建,然后“写入”文件。
    2. 文件夹和文件示例:

      var folderName = "StackOverflow";
      using (var driveId = DriveClass.DriveApi.GetRootFolder(_googleApiClient))
      using (var query = new QueryClass.Builder().AddFilter(Filters.And(Filters.Eq(SearchableField.Title, folderName), Filters.Eq(SearchableField.Trashed, false))).Build())
      using (var metaBufferResult = await driveId.QueryChildrenAsync(_googleApiClient, query))
      {
          if (metaBufferResult.Status.IsSuccess)
          {
              DriveId folderId = null;
              foreach (var metaData in metaBufferResult.MetadataBuffer)
              {
                  if (metaData.IsFolder && metaData.Title == folderName)
                  {
                      folderId = metaData.DriveId;
                      break;
                  }
              }
      
              IDriveFolder driveFolder = null;
              switch (folderId)
              {
                  case null: // if folder not found, create it and fall through to default
                      using (var folderChangeSet = new MetadataChangeSet.Builder().SetTitle(folderName).Build())
                      using (var folderResult = await driveId.CreateFolderAsync(_googleApiClient, folderChangeSet))
                      {
                          if (!folderResult.Status.IsSuccess)
                          {
                              Log.Error(TAG, folderResult.Status.StatusMessage);
                              break;
                          }
                          driveFolder = folderResult.DriveFolder;
                      }
                      goto default;
                  default:
                      driveFolder = driveFolder ?? folderId.AsDriveFolder();
      
                      // create your file in the IDriveFolder obtained, 
                      using (var contentResults = await DriveClass.DriveApi.NewDriveContentsAsync(_googleApiClient))
                      {
                          if (contentResults.Status.IsSuccess)
                          {
                              using (var writer = new OutputStreamWriter(contentResults.DriveContents.OutputStream))
                              {
                                  writer.Write("StackOverflow Rocks");
                                  using (var changeSet = new MetadataChangeSet.Builder()
                                      .SetTitle("StackOverflow Rocks")
                                      .SetStarred(true)
                                      .SetMimeType("text/plain")
                                      .Build())
                                  using (var driveFileResult = await driveFolder.CreateFileAsync(_googleApiClient, changeSet, contentResults.DriveContents))
                                  {
                                      if (driveFileResult.Status.IsSuccess)
                                          Log.Debug(TAG, "File created, open https://drive.google.com to review it");
                                      else
                                          Log.Error(TAG, driveFileResult.Status.StatusMessage);
                                  }
                              }
                          }
                      }
                      driveFolder.Dispose();
                      break;
              }
              folderId?.Dispose();
          }
          else
          {
              Log.Error(TAG, metaBufferResult.Status.StatusMessage);
          }
      }
      

      备注:

      • 在后台线程上执行此操作
      • 云端硬盘允许多个同名文件/文件夹(Title
        • 如果要替换一个
        • ,请查询现有文件
        • 查询现有文件夹,除非您确实具有相同Title
        • 的多个文件夹
      • Trash中的文件夹和文件是返回的查询,除非被排除。
      • 使用Using块和Dispose以避免泄漏