Not_Bluffing
Not_Bluffing

Reputation: 78

Xamarin Community Toolkit CameraView video file location supplied becomes invalid IOS and Android

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);
    }

Upvotes: 0

Views: 289

Answers (1)

Renji
Renji

Reputation: 165

I faced the same problem and it took a while for me to fix it. On every app relaunch the base path appears to be changed especially in iOS. Hence there is no point in saving the complete path to the DB, just the file name is enough.

The solution is to build the path at run time using the same base path you built while saving the image/video.

For eg: I save files to Environment.SpecialFolder.MyDocuments

You can just combine this path with your file name every time when you would like to access the file and it solves the issue.

Upvotes: 0

Related Questions