Help to upload img file to Google drive.

roberto0953roberto0953 roberto grazianoUSMember ✭✭

Hi i want to upload a image to google drive from my android app.

App take a picture and store it in:

public static File _file;

I want to upload _file to Google drive

I connect and I create a empty jpg file with this code:

`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();
    }
}

}
}
`

HOW TO FILL THIS EMPTY FILE?

Thank you!

Sorry for my bad english.

Answers

Sign In or Register to comment.