-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。
對不起,我的英語不好。
問題標題,你問如何上傳文件到谷歌驅動器。但有問題的身體你問如何將截圖保存到文件中。那麼你真正的問題是什麼? –
我想上傳App._File。我認爲這是:創建一個空的JPEG文件,然後寫它。你有什麼想法如何解決?你知道最好的方法嗎? –