question

JavanJoel-7907 avatar image
0 Votes"
JavanJoel-7907 asked JavanJoel-7907 answered

MediaPicker - Do I have to duplicate the video/image locally?

I'm using the Xamarin.Essentials.MediaPicker to choose images and video to "attach" to notes in my app. I would like these to be referenced locally so they can just open it whenever.

However for iOS, at least, it seems when I open and close the app the reference to the file is lost. Also PickPhotoAsync only has a relative reference to the file, so I cannot even view it using the absolute path because there is no absolute path.

According to this page:
https://docs.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=ios

... in the example code they save the image to the local CacheDirectory. I don't want to nor do I see a need to duplicate the file, as that is a waste of resources on the device (especially for video).

For Android this doesn't seem to be an issue. And for UWP it isn't an issue unless I want to play video using the MediaElement (from Xamarin CommunityToolkit).

My question is... do I need to save these files to the local CacheDirectory or is there something I'm missing?

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

For image we can directly set image from the file stream .

var photo = await MediaPicker.PickPhotoAsync();
            using (var stream = await photo.OpenReadAsync())
            {
                image.Source = ImageSource.FromStream(()=>{
                    return stream;
                });
            }


For video We need to the file path and set on MediaElement , does (await MediaPicker.PickVideoAsync()).FullPath; has value or not ?

0 Votes 0 ·
JavanJoel-7907 avatar image
0 Votes"
JavanJoel-7907 answered

@ColeXia-MSFT ... they attach the image to an internal note which can be accessed at any time later. True I can open the stream right away but if I want to open it at a later date it seems I need to save it to the local application folder.

Also "FullPath" , in iOS, only has a relative value. So the "FullPath" value will be something like "SDS892-SAD343-SDSA.png"

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ColeXia-MSFT converted comment to answer

For image : We can store the stream data as global variable in memory (e.g in App.cs).

For Video : It seems that we have to store it in local application folder.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.