我想知道是否有任何方法可以将应用程序中的图像以Xamarin格式上传到Google云端硬盘,我使用此链接关注了Google云端硬盘API
https://developers.google.com/drive/api/v3/manage-uploads
但是在xamarin中这种方法不起作用,某些库不可用
这是我的代码
var image = new Image
{
Source = ImageSource.FromFile(filepath)
};
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = "photo.jpg"
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(filepath,
System.IO.FileMode.Open))
{
request = DriveService.Files.Create(
fileMetadata, stream, "image/jpeg");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);
DriveService向我显示编译错误,我不知道这种方式是否行得通,有帮助吗?
答案 0 :(得分:2)
Xamarin.iOS和Xamarin.Android中提供了Google Drive API。因此,您可以使用DependencyService
并在iOS和Android平台上上传图像。
在表单中,创建界面
public interface IUploadFile
{
void UploadFile(string filePath,string fileName);
}
iOS实现
[assembly: Dependency(typeof(UploadFileImplementation))]
namespace xxx.iOS
{
public class UploadFileImplementation : IUploadFile
{
public UploadFileImplementation () { }
public UploadFile(string filePath,string fileName)
{
// upload file here
}
}
}
Android实现
[assembly: Dependency(typeof(UploadFileImplementation))]
namespace xxx.Droid
{
public class UploadFileImplementation : IUploadFile
{
public UploadFileImplementation () { }
public UploadFile(string filePath,string fileName)
{
// upload file here
}
}
}
您可以从nuget下载iOS和Android的Google Drive API。它为sample提供了在本地平台上上传文件的方法。