dpfauwadel
dpfauwadel

Reputation: 3934

Delete Image from documents Directory ios Xamarin

I save a picture in my documents Directory with this code

var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;

var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary;

// This bit of code saves to the application's Documents directory, doesn't save metadata

NSData imgData = photo.AsJPEG();

NSError err = null;

if (imgData.Save(jpgFilename, false, out err))

{

        Console.WriteLine("saved as " + jpgFilename);
} else {

        Console.WriteLine("NOT saved as" + jpgFilename + " because" + err.LocalizedDescription);

}

Then I get my image to see it with this code

var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg");

        UIImage currentImage = UIImage.FromFile (jpgFilename);

And now I want to delete this image from my documents directory I try this code

var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg");

NSFileManager nsf = new NSFileManager ();

NSError error = null;

nsf.Remove (jpgFilename, out error);

How can I manage to delete this image

Thank you

Upvotes: 1

Views: 2581

Answers (1)

dpfauwadel
dpfauwadel

Reputation: 3934

The answer is more simple than that

public void deleteImageInDirectory()
    {
        var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string jpgFilename = System.IO.Path.Combine (documentsDirectory, "Photo.jpg");
        System.IO.File.Delete (jpgFilename);
    }

Upvotes: 1

Related Questions