Skip to content Skip to sidebar Skip to footer

Upload Image Files Using Google Drive Api

Trying to upload an image file (already existing) from a user's phone (Android) on his google drive via my app using drive API and Xamarin (C#). There are several solutions to uplo

Solution 1:

upload image files using google drive Api

You could use the following codes to upload the image file to Google Drive :

DriveClass.DriveApi.NewDriveContents(client).SetResultCallback(newSystem.Action<IDriveApiDriveContentsResult>((result)=> {

            //Create MetadataChangeSetMetadataChangeSetmetadataChangeSet=newMetadataChangeSet.Builder()
                .SetTitle("Test.jpg")
                .SetMimeType("image/jpeg").Build();

            varcontents= result.DriveContents;

            //push the bitmap data to DriveContents
            bitmapFromYourPicture.Compress(CompressFormat.Jpeg, 1, contents.OutputStream);
            IntentSenderintentSender= DriveClass.DriveApi
                    .NewCreateFileActivityBuilder()
                    .SetInitialMetadata(metadataChangeSet)
                    .SetInitialDriveContents(contents)
                    .Build(client);

            StartIntentSenderForResult(intentSender, 2, null, 0, 0, 0);
        }));

If you choose the image picture by using intent you could get the picture Bitmap like this :

protectedoverridevoidOnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == 0)
    {
        var uri = data.Data;

        System.IO.Stream image_stream = ContentResolver.OpenInputStream(uri);
        Bitmap getBitmap = BitmapFactory.DecodeStream(image_stream);
    }
}

Post a Comment for "Upload Image Files Using Google Drive Api"