jfversluis / FilePicker-Plugin-for-Xamarin-and-Windows

FilePicker Plugin for Xamarin and Windows
MIT License
157 stars 80 forks source link

File corruption on Android 10 #203

Closed tomreich closed 4 years ago

tomreich commented 4 years ago

Got a headscratcher here for you. I'm picking zip files and using GetStream() to get the file contents. On all Android 9 and below devices I've tested, it's working fine with no issues. On Android 10, the stream consistently gets corrupted -- eventually, bytes in the stream start to disappear. I've tried on multiple devices with multiple files (ranging from 10mb up to 50) and this consistently seems to happen.

To repro this, I've got a valid ~43mb zip file called inputfile.zip. I copied this file to the Android 10 device and used the code below to pick it using FilePicker and copy the stream to a new file, outputfile.zip. I then extracted outputfile.zip from the android device and noted that it is corrupt. Interestingly, both inputfile.zip and outputfile.zip have the same byte count (45499125 - see screenshot) but if I run a binary diff on both files, you can see around location 02AF 98C0, the hex "EC" mysteriously goes missing (see screenshot).

var file = await CrossFilePicker.Current.PickFile();
if (file == null)
  return;

// Copy the file into the local context so we can manipulate it.
var internalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "outputfile.zip");
using (var writer = File.OpenWrite(internalPath))
{
    file.GetStream().CopyTo(writer);
}

I've also tried a similar test using DataBytes rather than GetStream() and I get a similar result.

Expected Behavior

inputfile.zip and outputfile.zip are identical.

Actual Behavior

Missing bytes (in this example around location 02AF 98C0)

Steps to Reproduce the Problem

  1. Upload zip file to device
  2. Run code listed above.
  3. Compare outputfile.zip to the zip you uploaded. Note that outputfile.zip is corrupt.

Specifications

bytecount hex diff

tomreich commented 4 years ago

Nothing to see here, folks. My Xamarin Forms reference in my app was pretty far out of date so I updated to the latest version and the issue seems to have resolved. Should anyone else encounter something similar, make sure you all your NuGet packages are up to date!

vividos commented 4 years ago

Glad to hear that your issue was resolved. It is very strange, though, that reading from a ContentProvider may skip bytes in the file...