Xamarin.Mobile Component multiple file saved getting deleted without calling delete function for iOS

662 Views Asked by At

We are using Xamarin.Mobile component for taking and storing photos in an local app directory. Now, its works fine when I am storing a single photo but if I am storing multiple photo and trying to access those, only last image exist, all other images are getting deleted. I don’t call delete function anywhere in my code.

We are making app for both iOS and android and we have some Core Class that are linked in both projects

When it enter the function as shown is the last call stack, only last image exists, all other image are getting deleted. The top three functions are from our Core Class which are linked in both iOS and Android.

I have enable iTunes file sharing, images are getting stored inside Documents folder and I am giving each image a different name.

here is the code for taking photos: Random rand1 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);

        picker = new MediaPicker();
        if( !picker.IsCameraAvailable )
            return;
        MediaPickerController controller = picker.GetTakePhotoUI (new StoreCameraMediaOptions {
            Name = "Ticket"+rand1.Next(1, 1000000)+".jpg",
            Directory = ""
        });
        this.PresentViewController( controller, true, null );
        controller.GetResultAsync().ContinueWith (t => {
            // We need to dismiss the controller ourselves
            this.PresentedViewController.DismissViewController (true, () => {
                // User canceled or something went wrong
                if (t.IsCanceled || t.IsFaulted)
                    return;

                // We get back a MediaFile
                MediaFile media = t.Result;



                ticket.imagePaths.Add( media.Path );

            });
        }, uiScheduler); 

Here is my Application Output: 2014-01-15 15:17:40.445 CCR_Ticket_System_iOS[2063:60b] File Exists in TicketRepo: /private/var/mobile/Applications/ABE44A50-7119-4BF9-8DF9-DC50C80E7591/Documents/Ticket235666.jpg 2014-01-15 15:17:42.384 CCR_Ticket_System_iOS[2063:60b] File Exists in TicketRepo: /private/var/mobile/Applications/ABE44A50-7119-4BF9-8DF9-DC50C80E7591/Documents/Ticket952511.jpg 2014-01-15 15:17:49.320 CCR_Ticket_System_iOS[2063:60b] File doesn't Exists in SaveTicket in DatabaseADO: /private/var/mobile/Applications/ABE44A50-7119-4BF9-8DF9-DC50C80E7591/Documents/Ticket235666.jpg 2014-01-15 15:17:49.323 CCR_Ticket_System_iOS[2063:60b] File Exists in SaveTicket in DatabaseADO: /private/var/mobile/Applications/ABE44A50-7119-4BF9-8DF9-DC50C80E7591/Documents/Ticket952511.jpg

Please let me know what I am doing wrong. Please let me know if you need more information. I am stuck on this problem for last 2 days.

1

There are 1 best solutions below

0
On

I just ran into this problem myself and saw that there was no solution posted yet. Here is what I figured out:

The problem has to do with the MediaPicker. When mediaPicker.PickPhotoAsync(), mediaPicker.TakePhotoAsync(), etc is called, it returns an object of type MediaFile. This MediaFile has a property called “Path” which contains a string with the path to the file. We store this string to use later.

The problem comes up when we realize how MediaPicker is implemented. As soon as the MediaFile is disposed of, it deletes the file that the MediaFile is associated with. Since we let the MediaPicker fall out of scope, the file gets deleted immediately after it is added to the list.

There are a couple solutions to this: 1) Keep a reference to the MediaFile until the file is no longer needed. 2) Copy the image to a new location so the MediaFile can be disposed of

Personally I think option 1 is a much better solution.