Forum Xamarin.Forms

Samsung Knox, Media Plugin, & File Streams

VegasJVegasJ USUniversity ✭✭
edited May 2017 in Xamarin.Forms

Hi there, I posted this over in the plugins forum but now that I think about it, it probably isn't an issue with the plugin. We currently have a Xamarin Forms project with some code using the Media plugin to take a picture and save that off to a database. This currently works great and as expected. We now have a requirement to have our Xamarin Forms project run inside a Samsung Knox container. However, the same code that we are using to take the picture doesn't work as expected inside the container. While we can take the picture fine, we then need to turn it into a byte array and resize it. However, when we try and get a Stream of the MediaFile (picture), the stream length is always 0 and thus our resize code fails. Here is the code that we are using:

public async void TakePicture()
        await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            await CoreMethods.DisplayAlert("No Camera", "No Camera on Device.", "OK");

        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            CompressionQuality = 92,
            SaveToAlbum = false,
            Name = "Deficiency.jpg"

        if (file == null)

        MemoryStream memoryStream = new MemoryStream();
        file.GetStream().CopyTo(memoryStream); <===== file.GetStream() results in a 0 length stream
        pictureAsBytes = memoryStream.ToArray();  

        byte[] resizedImage = pictureAsBytes; 
        resizedImage = DependencyService.Get<IImageResizer>().ResizeImage(pictureAsBytes, 1000, 1000);

        DeficiencyPictureViewModel newPicture = new DeficiencyPictureViewModel(new DeficiencyPicture());
        newPicture.Id = Deficiency.GlobalId.ToString();
        newPicture.ByteData = resizedImage;
        IFile ifile = FileSystem.Current.GetFileFromPathAsync(file.Path).Result;
        await ifile.DeleteAsync();

    catch (Exception e)
        await CoreMethods.DisplayAlert("Error Taking Picture", "There was a problem taking the picture. " + e.Message + e.StackTrace, "OK");

I have even tried to create a memory stream from the picture file itself, but the stream length is 0 here as well as well.

var image = await FileSystem.Current.GetFileFromPathAsync(file.Path);
var stream = await image.OpenAsync(FileAccess.Read);
var myBytes = new byte[stream.Length];
stream.Read(myBytes, 0, (int)stream.Length);

await CoreMethods.DisplayAlert("stream", "stream length " + stream.Length, "OK");           <======= length is 0
await CoreMethods.DisplayAlert("myBytes", "myBytes length " + myBytes.Length, "OK");    <======= length is 0

Any idea why my streams all have a length of zero?



  • VegasJVegasJ USUniversity ✭✭

    I found something strange. After I take a pic using the Cross Media Plugin, I get the confirmation screen with the picture I took and a "Retry" or "Ok" button. If I tap on "Retry" or hit the hardware back button (Galaxy Tab S2) the 2nd time around taking the pic, everything works. The length of the stream I get from the MediaFile returned from the plugin actually has a length to it instead of 0.

Sign In or Register to comment.