Where to save a picture in iOS and then to show it in app

jstuardojstuardo Member ✭✭

Hello,

I have a project for Android and iOS that needs to show a user image and video in a view.

In Android this was simple. I could just get the path using

System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryPictures);

And then I used System.IO to check file existence and finally, show that in the view (shared project).

What should be the equivalent in iOS?

When I plug the target iPhone in a USB port and browse its files, I only see one folder called DCIM which contains some folders with pictures.

Can I use that folder? how can I access it from the iOS project?

I tried with:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonPictures);

But it returns an empty string.

Any help, please?

Thanks

Jaime

Best Answer

  • jstuardojstuardo ✭✭
    Accepted Answer

    Hello,

    to make it more simple, I created a solution folder and I saved a video and a picture inside. I set properties to "Content" build action and then I could concatenate that path to NSBundle.MainBundle.BundlePath so that I could access the files at run time.

    Maybe this is not a best practice solution but this is a demo for a possible customer where I had to save the video and the image using folder explorer and then to show them in a view. By using MyDocuments folder, I could not see it in folder explorer.

    Regards
    Jaime

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    As I said in this case: https://forums.xamarin.com/discussion/161346/how-to-get-data-files-folder-in-xamarin-forms#latest, iOS has three folders to store data. Generally, we use documents:

    var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    
  • jstuardojstuardo Member ✭✭
    Accepted Answer

    Hello,

    to make it more simple, I created a solution folder and I saved a video and a picture inside. I set properties to "Content" build action and then I could concatenate that path to NSBundle.MainBundle.BundlePath so that I could access the files at run time.

    Maybe this is not a best practice solution but this is a demo for a possible customer where I had to save the video and the image using folder explorer and then to show them in a view. By using MyDocuments folder, I could not see it in folder explorer.

    Regards
    Jaime

  • LandLuLandLu Member, Xamarin Team Xamurai

    Please notice this is a mobile application why do you want to browse it using PC? You can do such operation on Android because it has external storage.
    But iOS is sandbox storage. Each app has its own sandbox and they are independent. We should not and cannot manipulate other applications' sandbox from the outside area. You can embed your resources in the project but it still can't be modified when it has been deployed on the iPhone.
    What we can do is using code to store data to storage programmatically instead of adding or deleting data using PC folder browser.

  • jstuardojstuardo Member ✭✭
    I needed that because this is a prototype project to show customer what the application can do. That's why I needed to copy, from the PC, a video and a picture to some accessible folder, not as an embedded resource.

    For the final application, that video and picture will be downloaded from a cloud server and stored in MyDocuments folder, but that will be other story.

    Regards
    Jaime
  • LandLuLandLu Member, Xamarin Team Xamurai

    Unfortunately, you can't just copy and paste a file to an iOS device like Android.
    Because iOS is a sandbox file system. Each application has its own storage which can't be accessed outside its own scope.

  • jstuardojstuardo Member ✭✭

    @LandLu But finally it was possible, so I answered the question by myself. I have created the folder in Visual Studio and just copied and pasted the files into that folder from the PC. I marked them to be as "Content" in the build action, and voilà.

    As I said, I did it using Visual Studio and not Windows Explorer. that was the only way to do that, and for now, it is no problem, since it is a prototype. In the final app the approach will be obviously different.

    Regards
    Jaime

Sign In or Register to comment.