使用 google drive Api 上传图片文件

编程入门 行业动态 更新时间:2024-10-24 16:22:37
本文介绍了使用 google drive Api 上传图片文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

尝试使用驱动器 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 17:56:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   文件   google   drive   Api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!