【发布时间】:2017-09-15 03:23:02
【问题描述】:
您好,我想从我的 Android 应用程序将图像上传到谷歌驱动器。
App 拍照并存储在:
public static File _file;
我想将 _file 上传到 Google 云端硬盘
我连接并使用以下代码创建一个空的 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 文件,然后将其写入。你知道如何解决吗?你知道最好的方法吗?
标签: c# android visual-studio xamarin google-api