0

I'm using the CameraView to capture a series of short videos. I want these videos to be viewable at a later time from within the app. I'm using the MediaCaptured event to get the path of captured videos and saving these paths in a SQLite db.

The first problem is on iOS, the path is valid while the app is open, but if I close the app and open it again the path is no longer valid. I've worked around this by copying the video to the AppDataDirectory but this seems bad because I haven't figured out how to delete the original so now two copies of the video exist.

The second problem is on both iOS and Android, after some amount of time (a few days or a week or more) these paths become invalid for some unknown reason.

What is the correct way to deal with this?

private void MediaCaptured(object obj)
    {
      MediaCapturedEventArgs args = obj as MediaCapturedEventArgs;

      string sPath = "";

      switch (Device.RuntimePlatform)
      {
        case Device.iOS:
          //On iOS args.Video.File returns a path that isn't valid when the app is restarted. To get around this issue I am copying the file to the App Data Directory.
          //The drawback is there are now two video files and I can't delete the original.
          var pathSplit = args.Video.File.Split('/');

          sPath = Path.Combine(FileSystem.AppDataDirectory, pathSplit[pathSplit.Length - 1]);
          File.Copy(args.Video.File, sPath);

          //TODO Should probalby be deleting the original video but not sure how (or if its possible).
          break;
        case Device.Android:
          sPath = args.Video.File; 
          break;
      }

      SavePathToDB(sPath);
    }
Not_Bluffing
  • 78
  • 2
  • 7
  • What is the complete original path? Sounds like its in a cache folder or temp folder. Also, why not simply delete the original location, after copying it? – ToolmakerSteve Jun 27 '22 at 23:16
  • The path from args.Video.File is "/var/mobile/Media/DCIM/100APPLE/IMG_0077.MOV" The new path becomes "/var/mobile/Containers/Data/Application/8F28EA48-005B-421C-94E6-EC23D0CD928B/Library/IMG_0077.MOV". I get an Operation not permitted error if I try to delete the original using File.Delete. I'm probably just missing a permission or something but even if I can delete the original file, this seems like a hack. I want to know the correct way to deal with this. – Not_Bluffing Jun 28 '22 at 00:00
  • Ok - don't delete then. Have you manually checked to see if the original file is there? Unclear whether the path is not valid because file went away, or because you can't access it now for some reason. – ToolmakerSteve Jun 28 '22 at 04:10
  • The files are still there however you've made me realise the GUID in the new path changed. No idea why it changed but the solution seems to be to store the filename without the full path and combine it with FileSystem.AppDataDirectory when I need it. Would still like to know if there is a better way to achieve this. – Not_Bluffing Jun 28 '22 at 21:02

0 Answers0