在 Google Drive 上使用存储访问框架存储文件 [英] Store file with the storage access framework on Google Drive

查看:22
本文介绍了在 Google Drive 上使用存储访问框架存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Android 存储访问框架存储一个文件.我的测试代码应该存储一个大小为 1000000 的文件hallo1".但只会创建一个文件大小为 0 的文件hallo1".我没有收到错误消息.保存在本地磁盘和 SD 卡上工作正常,只有谷歌驱动器不起作用.代码:

I want to store a file with the Android storage access framework. My test code should store a file "hallo1" with the size of 1000000. But only a file "hallo1" with the filesize 0 will be created. I get no error message. The saving on local disc and sd-card works fine only google drive does not work. The Code:

        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 存储访问框架中的文件选择器

输入文件名(代码中默认为hallo1")

data.Data 返回一个内部 Android.Net.Uri.

data.Data gives an internal Android.Net.Uri back.

有人知道问题出在哪里吗?

Somebody an idea where is the problem?

推荐答案

我找到了解决方案:

        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 的直接写入失败了?

Somebody a idea why this works and the direct writing with the System.IO.Stream failed?

这篇关于在 Google Drive 上使用存储访问框架存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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