使用 google drive Api 上传图片文件 [英] upload image files using google drive Api

查看:79
本文介绍了使用 google drive Api 上传图片文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用驱动器 API 和 Xamarin (C#) 通过我的应用程序从用户的手机 (Android) 上传图像文件(已存在)到他的谷歌驱动器上.有几种上传解决方案,但它们都显示上传新文件..例如

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 upload but they all are showing to upload a new file.. for example

Google Drive API 实现 Xamarin Android

使用 Xamarin.Android 将文件上传到谷歌驱动器文件夹

@sushihangover 给出的解决方案都创建了一个新的文本文件,然后通过 v3 API 上传它.我也查看了 API,但无法解决它..

both the solution given by @sushihangover create a new text file and then upload it via v3 API. I looked into the API as well but couldn't sort it out..

推荐答案

使用google drive Api上传图片文件

upload image files using google drive Api

您可以使用以下代码将图片文件上传到 Google Drive :

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

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

            //Create MetadataChangeSet
            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                .SetTitle("Test.jpg")
                .SetMimeType("image/jpeg").Build();

            var contents = result.DriveContents;

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

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

如果您使用意图选择图像图片,您可以获得这样的图片Bitmap:

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

protected override void OnActivityResult(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);
    }
}

这篇关于使用 google drive Api 上传图片文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆