在Windows Phone 8.1中的“共享”文件夹中保存并打开文档文件

时间:2015-12-02 10:29:59

标签: xamarin windows-phone-8.1

在我的Windows Phone 8.1 + WIndows Store 8.1应用中,需要下载该文件并保存到某个共享位置。然后需要打开相同的文件,用户可以通过合适的原生/安装的应用程序选择/打开。

示例:我可以下载.pdf,然后命令在Adobe Acrobat /用户提供的应用程序中打开此文件。

下载到LocalFolder和RoamingFolder工作正常,但打开它时会出现拒绝访问错误。

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.CreateFileAsync(fileName,, CreationCollisionOption.ReplaceExisting); 
stream.Position = 0;
stream.CopyTo(await file.OpenStreamForWriteAsync());
return file.Path;

稍后,调用以下函数以从上面的代码中打开下载的文档

Windows.System.Launcher.LaunchUriAsync(new Uri(fileName));

请告诉我,如何将文件保存到任何共享文件夹或允许其他应用访问并打开的权限。提前谢谢。

请注意,虽然答案没有区别,但我在Xamarin内部的本机Windows App解决方案中使用它。我已经在Android原生中实现了类似的功能,效果很好。

1 个答案:

答案 0 :(得分:0)

后来我发现访问被拒绝错误不是由于本地或漫游文件夹,而是因为文件在创建时被代码保持打开。

所以代码被替换为:

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); 
stream.Position = 0;
using (Stream fileStream = await file.OpenStreamForWriteAsync())
        {
            stream.CopyTo(fileStream);
        }
return file.Path;

并且还要从app打开文件,将代码替换为:

StorageFile fileToLaunch = await StorageFile.GetFileFromPathAsync(fileName);
bool done = await Windows.System.Launcher.LaunchFileAsync(fileToLaunch, new Windows.System.LauncherOptions { DisplayApplicationPicker = true });

谢谢。