Search code examples
c#uwpwindows-store-apps

Trying to save a copy of a picture selected by FileOpenPicker and saved by FileSavePicker


This is the code i have so far that that opens the picture and tries to save it any information on why it's not working would be great thanks.

        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation =PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {



            FileSavePicker savePicker = new FileSavePicker();
            savePicker.SuggestedStartLocation = 
             PickerLocationId.PicturesLibrary;
            savePicker.FileTypeChoices.Add("jpeg image", new List<string>() 
            { ".jpg" });
            savePicker.SuggestedFileName = "Photo";

            string token = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
            StorageFile SaveFile = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);

            StorageFile savefile = await savePicker.PickSaveFileAsync();

            if (SaveFile != null)
            {
                 await FileIO.WriteTextAsync(SaveFile, SaveFile.Name);
            }

        }

Solution

  • You store the opened file into FutureAccessList and then immediately retrieve it as SaveFile variable. Right below you create a saveFile which I guess is what you wanted to use, but in the WriteTextAsync method you pass SaveFile as the target, not saveFile.

    You should definitely improve your naming convention, having two variables differing only in casing is very error prone. Furthermore, local variables should always start with lowercase letter.