Forum Xamarin.Forms

Saving Images using Xamarin Forums

JacobSteeleJacobSteele USMember ✭✭
edited October 2014 in Xamarin.Forms

Hey Xamarin,

I have been searching through the forums, there were some questions but no answers.

How do I save an image using Xamarin Forums, is this feature enabled?

async Task DoTakePhoto () { this.mediaPicker = DependencyService.Get<IMediaPicker>(); var media = await this.mediaPicker.TakePhotoAsync( new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Rear, MaxPixelDimension = 900 } ); image.Source = ImageSource.FromStream(() => media.Source ); }

Currently using this code to get the image. However I do not see a save option. I am using the Xamarin Forums Labs and the IMediaPicker. Does the saving need to be handled custom on iOS and Android separately?

Posts

  • RyanHatfieldOldRyanHatfieldOld USMember, Insider ✭✭✭

    @JacobSteele‌

    You might not have many answers to this 'cause it's a Xamarin Forms Labs question not really a Xamarin Forms question.

    What's the return type of media? Assuming it's a stream, you'll need to implement a file dependency service and save it that way. I tend to use a service that exposes the read/write stream but some people pass byte arrays around and write those.

    Here's an example of a file saving interface I use, derived heavily from Xamarin's Example on file manipulation (which you should probably browse through)

    public interface IFileReadWrite
    {
        Stream GetWriteStream(string fileName);
        Stream GetReadStream(string fileName);
        bool FileExists(string fileName);
    }
    
    //and the android implementation
    
    [assembly: Dependency(typeof(FileReadWrite_Android))]
    namespace SomeNamespace.Interfaces
    {
        public class FileReadWrite_Android : IFileReadWrite
        {
            public Stream GetReadStream(string fileName)
            {
                var filePath = GetFilePath(fileName);
                return File.OpenRead(filePath);
            }
    
            public Stream GetWriteStream(string fileName)
            {
                var filePath = GetFilePath(fileName);
                var stream = File.OpenWrite(filePath);
                return stream;
            }
    
            public bool FileExists(string fileName)
            {
                var filePath = GetFilePath(fileName);
                return File.Exists(filePath);
            }
    
            private string GetFilePath(string fileName)
            {
                var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                return Path.Combine(documentsPath, fileName);
            }
        }
    }
    

    Make sure to take heed:

    iOS 8 has changed the directory structure used by apps (see Apple's note). Use the following code in iOS 8 instead of the Environment class
    var documentsPath = NSFileManager.DefaultManager.GetUrls (NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User) [0];

    Ryan

  • JacobSteeleJacobSteele USMember ✭✭

    Thanks a ton Ryan :D

  • NielsCupNielsCup NLMember ✭✭✭
    edited November 2014

    I had the same issue and I am using the Cirrious.MvvmCross.Plugins.File plugin for this. (I had used this in another project so I decided to give it a try in my Xamarin.Forms project and it works).

Sign In or Register to comment.