1

Step 1: Get the file I would like to save (in this case a small png).

var fileBytes = getFileBytes();

Step 2: Get a content uri from google drive.

var intent = new Intent(Intent.ActionCreateDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType(mimeType);
intent.PutExtra(Intent.ExtraTitle, fileNameWithExtension);
StartActivityForResult(Intent.CreateChooser(intent, "Select Save Location"), 43);

Step 3: Create a FileOutputStream to handle writing for the content provider.

protected override async void OnActivityResult (int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult (requestCode, resultCode, data);

    if (resultCode == Result.Canceled && requestCode == 43) {
        using (var pFD = ContentResolver.OpenFileDescriptor(data.Data, "w"))
        using (var outputSteam = new FileOutputStream(pFD.FileDescriptor))
        {
            await outputSteam.WriteAsync(fileBytes);
        }
    }
}

This code works if I save the file locally. But when I save the file to Google Drive the file is there but it has 0 bytes. This is concerning because if i'm doing this right and it doesn't work on Google Drive then who knows if it will work on other content providers.

To be clear, it creates the file in Step 2. So is the code in Step 3 correct? Again, it does work if I save the file locally.

6
  • The uri you get from Google Drive is it a local path on the device or? Did you request permission to write to external storage before these actions? Jun 25, 2018 at 5:37
  • That is a bug in the way Xamarin wraps Streams. Xamarin wraps the Java streams and returns a .Net steam and does not expose the actual Java stream...it drives me crazy as those are fubar for some Content Providers (Google Drive is just one that does not work properly w/ Xamarin). You have to only Java-based streams and then it will work correctly. Jun 25, 2018 at 7:15
  • @Cheesebaron This is a content provider uri. You are granted "rw" temporary perms via the Content Provider (Drive in this case) since ActionCreateDocument was used. This is actually a "bug" in Xamarin converted Java->C# streams, been there for years.... Jun 25, 2018 at 7:19
  • @SushiHangover how do you get at the Java streams if they aren't exposed? btw I did try flushing and disposing the outputStream manually but obviously that didn't help.
    – user875234
    Jun 25, 2018 at 12:21
  • @user875234 I've tried that also :-( .Flush..Close/ using ContentResolver.OpenFileDescriptor vs. using the .FileDescriptor to get a stream, etc... It all results in the same thing, a zero byte file in some providers (FileProvider-based ones seem to always work, but the virtual ones like Drive typically fail). In Java, the stream code always works. I either use Java class reflection to get the openOutputStream method from the resolver and the OutputStream and InputStream classes or just write it in Java (Jar-library) and wrap it in a X.A binding library. Jun 25, 2018 at 13:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.