2017-09-15 67 views
-1

嗨,我想從我的Android應用程序上傳到谷歌驅動器的圖像。我需要上傳圖像_文件從谷歌驅動器從Android應用程序c#

應用拍照,並將其存儲在:

public static File _file; 

我想_file上傳到谷歌驅動

我接,我創建這個代碼空JPG文件:

`using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using MySql.Data.MySqlClient; 
using System.Data; 
using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v3.Data; 
using Google.Apis.Services; 
using Google.Apis.Util.Store; 
using Google.Apis.Upload; 
using Google.Apis.Http; 
using Android.Util; 
using Android.Gms.Common; 
using Android.Gms.Common.Apis; 
using Android.Gms.Drive; 
using System.Threading.Tasks; 
using Java.IO; 
using Google.Apis.Drive.v3; 

namespace Fotonet 
{ 
[Activity(Label = "Activity2")] 
public class Activity2 : Activity, GoogleApiClient.IConnectionCallbacks, IResultCallback, IDriveApiDriveContentsResult 
{ 
const string TAG = "Fotonet"; 
const int REQUEST_CODE_RESOLUTION = 3; 

GoogleApiClient _googleApiClient; 

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 

    SetContentView(Resource.Layout.prova2); 

    Button button = FindViewById<Button>(Resource.Id.myButton); 
    button.Click += delegate 
    { 
     Activity1.Global.count = 2; 

     if (_googleApiClient == null) 
     { 
      _googleApiClient = new GoogleApiClient.Builder(this) 
       .AddApi(DriveClass.API) 
       .AddScope(DriveClass.ScopeFile) 
       .AddConnectionCallbacks(this) 
       .AddOnConnectionFailedListener(OnConnectionFailed) 
       .Build(); 
     } 
     if (!_googleApiClient.IsConnected) 
      _googleApiClient.Connect(); 



    }; 



} 

protected void OnConnectionFailed(ConnectionResult result) 
{ 
    Log.Info(TAG, "GoogleApiClient connection failed: " + result); 
    if (!result.HasResolution) 
    { 
     GoogleApiAvailability.Instance.GetErrorDialog(this, result.ErrorCode, 0).Show(); 
     return; 
    } 
    try 
    { 
     result.StartResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
    } 
    catch (IntentSender.SendIntentException e) 
    { 
     Toast.MakeText(this, e.ToString(), ToastLength.Short).Show(); 
    } 
} 

public void OnConnected(Bundle connectionHint) 
{ 
    Toast.MakeText(this, "Connesso a Google", ToastLength.Short).Show(); 
    DriveClass.DriveApi.NewDriveContents(_googleApiClient).SetResultCallback(this); 
    Activity1.Global.count += 1; 
    //StartActivity(typeof(MainActivity)); 
} 

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
{ 
    base.OnActivityResult(requestCode, resultCode, data); 
    if (requestCode == REQUEST_CODE_RESOLUTION) 
    { 
     switch (resultCode) 
     { 
      case Result.Ok: 
       _googleApiClient.Connect(); 
       break; 
      case Result.Canceled: 
       Toast.MakeText(this, "Unable to sign in", ToastLength.Short).Show(); 
       Log.Error(TAG, "Unable to sign in, is app registered for Drive access in Google Dev Console?"); 
       break; 
      case Result.FirstUser: 
       Toast.MakeText(this, "Unable to sign in: RESULT_FIRST_USER", ToastLength.Short).Show(); 
       Log.Error(TAG, "Unable to sign in: RESULT_FIRST_USER"); 
       break; 
      default: 
       Log.Error(TAG, "Should never be here: " + resultCode); 
       return; 
     } 
    } 
} 

void IResultCallback.OnResult(Java.Lang.Object result) 
{ 
    var contentResults = (result).JavaCast<IDriveApiDriveContentsResult>(); 
    if (!contentResults.Status.IsSuccess) // handle the error 
     return; 
    Task.Run(() => 
    { 
     var image = new FileOutputStream(App._file); 

     //writer.Write("Stack Overflow"); 
     //writer.Close(); 
     MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
       .SetTitle("Fotonet.jpg") 
       .SetMimeType("image/jpeg") 
       .Build(); 
     DriveClass.DriveApi 
        .GetRootFolder(_googleApiClient) 
        .CreateFile(_googleApiClient, changeSet, contentResults.DriveContents); 
    }); 
} 

public void OnConnectionSuspended(int cause) 
{ 
    throw new NotImplementedException(); 
} 

public IDriveContents DriveContents 
{ 
    get 
    { 
     throw new NotImplementedException(); 
    } 
} 

public Statuses Status 
{ 
    get 
    { 
     throw new NotImplementedException(); 
    } 
} 
} 
} ` 

如何填寫這個空文件?

謝謝!

當我嘗試上傳它時創建一個空文件jpg。

對不起,我的英語不好。

+1

問題標題,你問如何上傳文件到谷歌驅動器。但有問題的身體你問如何將截圖保存到文件中。那麼你真正的問題是什麼? –

+0

我想上傳App._File。我認爲這是:創建一個空的JPEG文件,然後寫它。你有什麼想法如何解決?你知道最好的方法嗎? –

回答

0

如何填寫這個空文件?

如果你只是想創建基本的圖片文件,然後使用下面的方法:

private Bitmap CreateBitmap(int w, int h) 
{ 
    Bitmap bitmap = Bitmap.CreateBitmap(w, h, Config.Argb8888); 
    int[] pixels = new int[w * h]; 
    for (int x = 0; x < w * h; x++) 
    { 
     pixels[x] = Color.Blue.ToArgb(); 
    } 
    bitmap.SetPixels(pixels, 0, w, 0, 0, w, h); 
    return bitmap; 
} 

而且你可以使用下面的代碼上傳的圖片文件,谷歌驅動器:

DriveClass.DriveApi.NewDriveContents(client).SetResultCallback(new System.Action<IDriveApiDriveContentsResult>((result)=> { 

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

      var contents = result.DriveContents; 
      //create the bitmap 
      Bitmap bitmap = CreateBitmap(800, 800); 
      //push the bitmap data to DriveContents 
      bitmap.Compress(CompressFormat.Jpeg, 1, contents.OutputStream); 
      IntentSender intentSender = DriveClass.DriveApi 
        .NewCreateFileActivityBuilder() 
        .SetInitialMetadata(metadataChangeSet) 
        .SetInitialDriveContents(contents) 
        .Build(client); 

      StartIntentSenderForResult(intentSender, 2, null, 0, 0, 0); 
     })); 
相關問題