Forum Xamarin.iOS

Unable to store/retrieve images from ios application due to sandboxing

al3xmeisteral3xmeister Member ✭✭

I'm trying to get images to persist to local storage, then retrieve and be displayed in a listview which works fine, except when I have to run the application again the 'sandbox' style of file storage in ios deletes my images. I have followed and others, to use relative paths, but still getting file not found exceptions for pcl local storage.

string LOCAL_STORAGE = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //
string fileName = filename; // "Image.jpg"; // System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
string filePath = System.IO.Path.Combine(LOCAL_STORAGE, "Images", fileName);
string relativePath = System.IO.Path.Combine("Images", fileName); // This is the path you save

Are there any working samples or alternatives?
Many thanks in advance.

Best Answer


  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you deleted the prior application when you want to run it again? The storage will be deleted when the app is uninstalled.
    If not, how did you retrieve your file? Application's sandbox file path will be changed each time the application has redeployed. So you need to use API to get the path instead of using an absolute path. Record your file's and subfolder's name and use Environment.GetFolderPath to combine the path.

  • al3xmeisteral3xmeister Member ✭✭
    I have already tried that, as per the code snippet, it seems as if the files either got deleted or replaced because its throwing the same exception. Have you got a working example of files persistent after storing locally and a new debugging session?
  • LandLuLandLu Member, Xamarin Team Xamurai
    edited July 2019

    Here is a simple demo which is used for storing and retrieving plain text.
    The text file can be read no matter how many times the project redeployed.

  • al3xmeisteral3xmeister Member ✭✭
    Hey man, thanks for that. With text files and all others it seems to be fine, however with images and videos (mp4) they don't persist. Can you please try with images?
Sign In or Register to comment.