Discussion Board

Results 1 to 8 of 8

Hybrid View

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    11

    Is it possible to use a FileSavePicker to save files from Isolated Storage to phone?

    I'm attempting to make my app size smaller by no longer packaging audio files within the app. All is well except for one function. I have FileSavePicker that worked fine when audio files were packaged with the app. Now however, I am unable to get it to work with saving from isolated storage. Ive searched all over and have not seen one example of this. Ive tried posting this at MSDN and even StackOverflow, but not getting any responses. So I thought Id try here. I figure someone out there has to know if this can be done and what I would need to change.

    As it stands now, I receive the following Exception: System.IO.FileNotFoundException at this line in my SoundData model:

    StorageFile file = await folder.GetFileAsync(SavePath);


    SoundData:

    Code:
    private async void ExecuteSaveSoundAs(string soundPath)
            {
                
                StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                StorageFile file = await folder.GetFileAsync(SavePath);
    
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedSaveFile = file;
                savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
                savePicker.ContinuationData.Add("SourceSound", SavePath);
                savePicker.SuggestedFileName = this.Title;
                savePicker.PickSaveFileAndContinue();
            }
       
            public SoundData()
            {
                SaveSoundAs = new RelayCommand<string>(ExecuteSaveSoundAs);            
            }
    App.xaml.cs:

    Code:
    private async void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActivatedEventArgs e)
            {
                var args = e as FileSavePickerContinuationEventArgs;
                if (args != null)
                {
                    StorageFile file = args.File;
                    if (file != null)
                    {
                        // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync. 
                        CachedFileManager.DeferUpdates(file);
                        // write to file 
                        var sourceFilePath = args.ContinuationData["SourceSound"].ToString();  
                        StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                        StorageFile sourceFile = await folder.GetFileAsync(sourceFilePath);
    
                        IRandomAccessStreamWithContentType sourceStream = await sourceFile.OpenReadAsync();
    
                     var bytes = new byte[sourceStream.Size];
    
                     var sourceBuffer = await sourceStream.ReadAsync(bytes.AsBuffer(), (uint)sourceStream.Size, Windows.Storage.Streams.InputStreamOptions.None);
    
                        await FileIO.WriteBufferAsync(file, sourceBuffer);
                        // Let Windows know that we're finished changing the file so the other app can update the remote version of the file. 
                        // Completing updates may require Windows to ask for user input. 
                        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                    }
                }
            }
    SoundModel:

    Code:
    private SoundGroup CreateSoftwareGroup()
            {
                SoundGroup data = new SoundGroup();
    
                data.Title = "OS/Software";
    
                data.Items.Add(new SoundData
                {
                    Title = "Test1",
                    FilePath = "https://xxxxx.test1.mp3",
                    Groups = "Test",
                    SavePath = "Test1.mp3"
                });
    
                data.Items.Add(new SoundData
                {
                    Title = "Test2",
                    FilePath = "https://xxxxx.Test2.mp3",
                    Groups = "Test",
                    SavePath = "Test2.mp3"
                });

  2. #2
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    126

    Re: Is it possible to use a FileSavePicker to save files from Isolated Storage to pho

    Code:
    StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                StorageFile file = await folder.GetFileAsync(SavePath);
    This is how you would get the file if it was in the app package, not isolated storage. For isolated storage, use:

    Code:
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("filename");
    //or ApplicationData.Current.LocalCache if you want it NOT to be backup up automatically... depends on where you saved it.

  3. #3
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    126

    Re: Is it possible to use a FileSavePicker to save files from Isolated Storage to pho

    After reading all of it again, I'm not sure I understand what you're doing exactly. It seems you are downloading a file from the internet and attempting to save it to the app's local storage. If so, you do NOT need a save file picker. The PickSaveFileAndContinue is only if you want to save it to an external directory that the user has access to outside of your app.

    Just save this way:

    Code:
    var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("name", CreationCollisionOption.ReplaceExisting);
    
    //now you have a storageFile to save to however you do it.
    Again, if you don't want these files backup up to OneDrive (by user settings), then use LocalCache instead.

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    11

    Re: Is it possible to use a FileSavePicker to save files from Isolated Storage to pho

    Sorry for the wording. Ive always been horrible at explaining things. The app downloads the sounds to isolated storage. That portion already works fine. Id like to use the FileSavePicker to download from isolated storage to a folder of the users choosing. I had success with this prior to using isolated storage, when all the sounds were packaged with the app.

    Almost working now. It brings up the folders but when I attempt to do anything at that point I get an exception:


    + $exception {System.IO.FileNotFoundException: The system cannot find the file specified.

    here on app.xaml.cs:
    IStorageFile sourceFile = await folder.GetFileAsync(sourceFilePath);


    I modified the code in my SoundData:

    Code:
    private async void ExecuteSaveSoundAs(string soundPath)
            {
    
                var file = await ApplicationData.Current.LocalFolder.GetFileAsync(SavePath);
    
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedSaveFile = file;
                savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
                savePicker.ContinuationData.Add("SourceSound", SavePath);
                savePicker.SuggestedFileName = this.Title;
                savePicker.PickSaveFileAndContinue();
            }
    Last edited by Mattsos; 2014-07-04 at 20:50.

  5. #5
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    126

    Re: Is it possible to use a FileSavePicker to save files from Isolated Storage to pho

    I don't have any experience with the "Application_ContractActivated" method you have in your App.xaml.cs. I've been using the ContinuationManager class that Microsoft has created. It's explained fairly well here.

    http://msdn.microsoft.com/en-us/libr.../dn631755.aspx

    Once this is in place and you have overrided OnActivation in App.xaml.cs, you can make the page that you need to return to (after picking a file or folder) implement IFileSavePickerContinuable. You then implement the following method:

    Code:
    public void ContinueFileSavePicker(Windows.ApplicationModel.Activation.FileSavePickerContinuationEventArgs args)
            {
                if (args.File != null)
              {
                  //do something with the storagefile
              }
            }
    However, the StorageFile that is returned is empty. You need to copy the data you want to save into that file. If you're copying from a local file, then just use:
    Code:
     await localFile.CopyAndReplaceAsync(pickedFile);

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    11

    Re: Is it possible to use a FileSavePicker to save files from Isolated Storage to pho

    Sorry, correction to my last email. I screwed up and had the wrong code in when I did the last test. I made some corrections.

    I also updated this:

    Code:
     var sourceFilePath = args.ContinuationData["SourceSound"].ToString();
                        StorageFolder folder = ApplicationData.Current.LocalFolder;
                        StorageFile sourceFile = await folder.GetFileAsync(sourceFilePath);

    It actually saves now. But the only issue is it does not give the option to rename the file before saving.

  7. #7
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    126

    Re: Is it possible to use a FileSavePicker to save files from Isolated Storage to pho

    The rename option is in the secondary menu of the folder picker UI. Just pull up the menu and you'll see it.

Similar Threads

  1. Replies: 10
    Last Post: 2014-07-04, 02:22
  2. FileSavePicker. How to code source path?
    By ChuckJ in forum Windows Phone General
    Replies: 2
    Last Post: 2014-06-07, 22:28
  3. Image in Isolated Storage as background
    By shinobi86 in forum Windows Phone General
    Replies: 1
    Last Post: 2013-12-09, 08:33
  4. Saving audio files from RMS to phone storage
    By a_nemoff in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2006-05-17, 19:04

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×