使用Google云端硬盘上的存储访问框架存储文件

urs32

我想使用Android存储访问框架存储文件。我的测试代码应存储大小为1000000的文件“ hallo1”。但是,只会创建文件大小为0的文件“ hallo1”。我没有收到错误消息。仅在Google驱动器上无法正常保存在本地光盘和SD卡上。编码:

        protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.myButton);

        button.Click += delegate
        {
            Intent intentCreate = new Intent(Intent.ActionCreateDocument);
            intentCreate.AddCategory(Intent.CategoryOpenable);
            intentCreate.SetType("audio/*");
            intentCreate.PutExtra(Intent.ExtraTitle, "hallo" + count);
            StartActivityForResult(intentCreate, 1);

            count++;
        };
    }

    protected override  void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        var stream = this.ContentResolver.OpenOutputStream(data.Data, "w");
        var b = new byte[1000000];
        stream.Write(b, 0, b.Length);
        stream.Flush();
        stream.Close();

        base.OnActivityResult(requestCode, resultCode, data);
    }

来自Android存储访问框架的Filepicker

输入文件名(代码中默认的“ hallo1”)

data.Data返回内部的Android.Net.Uri。

有人知道问题出在哪里?

urs32

我找到了解决方案:

        protected override  void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {

        // this works fine:
        using (System.IO.Stream stream = this.ContentResolver.OpenOutputStream(data.Data, "w"))
        {
            using (var javaStream = new Java.IO.BufferedOutputStream(stream))
            {
                var b = new byte[1000000];
                javaStream.Write(b, 0, b.Length);
                javaStream.Flush();
                javaStream.Close();
            }
        }

        // the direct writing on the System.IO.Stream failed:
        //using (System.IO.Stream stream = this.ContentResolver.OpenOutputStream(data.Data, "w"))
        //{
        //    var b = new byte[1000000];
        //    stream.Write(b, 0, b.Length);
        //    stream.Flush();
        //    stream.Close();
        //}

        base.OnActivityResult(requestCode, resultCode, data);
    }

有人知道为什么这可行并且直接用System.IO.Stream编写失败?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google云端硬盘文件如何没有“云端硬盘上存储的任何内容”?

如何使用Apps脚本访问Google云端硬盘上受密码保护的PDF文件

使用Adobe Acrobat在Google云端硬盘上打开.pdf文件

什么是Google云端存储上的“文件访问”?

Google 云端硬盘上传文件

复制Google云端硬盘上的文件

在Google云端硬盘上显示HTML文件?

使用缓冲区或流将Webshot存入Google云端硬盘而不存储中间文件?

使用Google云端存储中的文件

Google云端硬盘或Google云存储

在Google App Engine Flex上运行时,使用Java将文件从Google存储桶移动到Google云端硬盘

如何从存储在硬盘上的ISO文件启动?

文件系统信息如何存储在硬盘上

从Google云端存储桶访问私有文件

无法使用Google App脚本上传到Google云端硬盘上的子文件夹

停用Google云端硬盘文件夹外部的Google云端硬盘上下文菜单

如何在Google云端硬盘上强制使用唯一的文件名?

如何使用vb.net在Google云端硬盘上上传文件?

Python 3.x-如何使用请求补丁更新Google云端硬盘上的CSV文件

如何获取在Google云端硬盘上的文件的完整路径?

读取Google云端硬盘上托管的csv文件

无法自动永久删除Google云端硬盘上的文件

使用ACTION_CREATE_DOCUMENT写入的文件在Google云端硬盘中为空,但在本地存储中为空

Google云端硬盘API /将文件属性存储为数字而不是字符串

使用Google Apps脚本将文件夹上载/命名到Google云端硬盘上的目标文件夹

Python Google云端硬盘存储空间配额

Google云端存储-文件未显示

Google云端存储:文件系统限制

如何在不使用双倍存储空间的情况下将Gmail邮件存储在Google云端硬盘中