Windows Phone 8.1 RT PickSaveFileAndContinue() method creates an empty file

236 Views Asked by At

I'm trying to save a file to the Documents folder using PickSaveFileAndContinue() method in WP 8.1 RT. Everything is happening fine except the file which gets saved is empty. When I get the file returned from the following code in OnActivated() method, it's size is zero.

Anyone?

var database = await    FileHelper.GetFileAsync(ApplicationData.Current.LocalFolder, DATABASE_NAME);
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("Database File", new List<string>() { ".db" });
savePicker.DefaultFileExtension = ".db";
savePicker.SuggestedFileName = DATABASE_NAME;
savePicker.SuggestedSaveFile = database;

After the location is picked, the following code is executed in App.xaml.cs. I tried doing this inside the same page using a ContinuationManager. But then result is same.

 protected async override void OnActivated(IActivatedEventArgs args)
    {
        byte[] buffer = null;
        if(args!=null)
        {
            if(args.Kind == ActivationKind.PickSaveFileContinuation)
            {
                var file = ((FileSavePickerContinuationEventArgs)args).File;//This is empty
                using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
                {
                    buffer = new byte[stream.Size];
                    using (DataReader reader = new DataReader(stream))
                    {
                        await reader.LoadAsync((uint)stream.Size);
                        reader.ReadBytes(buffer);
                    }
                }

                if (file != null)
                {
                    CachedFileManager.DeferUpdates(file);
                    await FileIO.WriteBytesAsync(file, buffer);
                    Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                }
            }
        }
        base.OnActivated(args);
    }
1

There are 1 best solutions below

1
On

That's expected. PickSaveFileAndContinue doesn't know what the app wants to save. It just provides an empty StorageFile. The app can then write whatever contents it wants to save into the file.