简体   繁体   中英

How to upload Image File to GoogleDrive In Xamarin forms

I Wondered If there is any way to upload image from application to Google Drive in Xamarin forms , I followed Google Drive API using this link

https://developers.google.com/drive/api/v3/manage-uploads

but in xamarin this way doesn't work some libraries is not available

This is my code

 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 show me compiling error and i don't know if this way will work or not , Any help ?

Google Drive API is available in Xamarin.iOS and Xamarin.Android.So you can use DependencyService and upload the image in iOS and Android platform.

in Forms ,creat the Interface

public interface IUploadFile
{
  void UploadFile(string filePath,string fileName);
}

iOS Implementation

[assembly: Dependency(typeof(UploadFileImplementation))]
namespace xxx.iOS
{

  public class UploadFileImplementation : IUploadFile
  {
    public UploadFileImplementation () { }

    public UploadFile(string filePath,string fileName)
    {
        // upload file here

    }
  }
}

Android Implementation

[assembly: Dependency(typeof(UploadFileImplementation))]
namespace xxx.Droid
{

  public class UploadFileImplementation : IUploadFile
  {
    public UploadFileImplementation () { }

    public UploadFile(string filePath,string fileName)
    {
        // upload file here

    }
  }
}

You can download Google Drive API for iOS and Android from nuget.And it provide sample how to upload file on native platform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM