1

I tried this below code, the audio file is saving but it shows 0 bytes, I tried a lot if any one know about this please help me...

WP8.1 Universal Apps

Code in Mainpage.cs:

private async void pick_Click(object sender, RoutedEventArgs e)
        {
            string path = @"Assets\Audio\DeviPrasad.mp3";
            StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile file = await folder.GetFileAsync(path);

            var st =await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            var size = st.Size;

            FileSavePicker savePicker = new FileSavePicker();
            //savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            savePicker.SuggestedSaveFile = file;
            savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
            savePicker.ContinuationData.Add("SourceSound", path);
            savePicker.SuggestedFileName = "DeviPrasad";

            savePicker.PickSaveFileAndContinue();            
        }

internal async void ContinueFileOpenPicker(FileSavePickerContinuationEventArgs e)
        {
            var file = e.File;
            var ff= file.Properties;
            if(file!=null)
            {
                CachedFileManager.DeferUpdates(file);                  
                await FileIO.WriteTextAsync(file, file.Name);                    
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }

Code In App.xaml.cs:

protected async override void OnActivated(IActivatedEventArgs args)
        {
            var root = Window.Current.Content as Frame;
            var mainPage = root.Content as MainPage;
            if (mainPage != null && args is FileSavePickerContinuationEventArgs)
            {
                mainPage.ContinueFileOpenPicker(args as FileSavePickerContinuationEventArgs);
            }
        }
  • possible duplicate of http://stackoverflow.com/questions/24048626/filesavepicker-saving-0-bytye-file-windows-phone-8 – Joseph Jul 16 '15 at 05:02
  • @Joseph your referred link having code for windows phone, but i am working with WP8.1 Universal apps –  Jul 16 '15 at 05:06
  • I hope there wont be much changes in that.Could you please try it out? – Joseph Jul 16 '15 at 06:47

1 Answers1

0

The FileSavePicker will not help you to write or copy the content of the file to the destination.

Actually, I think you just simply copy the following code from somewhere but don't exactly know what it is doing.

await FileIO.WriteTextAsync(file, file.Name);  

This is to write things to the file, and seems it is from a sample which handles txt file. For your case, we need to do the following things:

  1. Add the source file path to ContinuationData in pick_Click event. Change the code to: savePicker.ContinuationData.Add("SourceSound", file.Path);

  2. Read the source file path and destination file in ContinueFileOpenPicker to write the content as below:

        var file = e.File;
        string soundPath = (string)e.ContinuationData["SourceSound"];
        //var ff = file.Properties;
    
        if (file != null)
        {
            CachedFileManager.DeferUpdates(file);
    
            StorageFile srcFile = await StorageFile.GetFileFromPathAsync(soundPath);
    
            await srcFile.CopyAndReplaceAsync(file);
    
            FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
        }
    
Alan Yao - MSFT
  • 3,284
  • 1
  • 17
  • 16