如何获得图片路径保存到WP8本地文件夹活瓷砖 [英] How to get image path saved to WP8 Local folder for Live Tile

查看:161
本文介绍了如何获得图片路径保存到WP8本地文件夹活瓷砖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新我的Windows Phone应用程序使用新的WP8文件存储API的过程( LocalFolder ),而不是WP7的API( IsolatedStorageFile )。



旧工作方法



下面是我如何成功地将图像保存到 /共享/ ShellContent 使用 IsolatedStorageFile 方式(注意文件夹/共享/ ShellContent / 保存图像时的前缀,也是 isostore:/共享/ ShellContent / 前缀更新动态磁贴时

 使用(IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
如果(myIsolatedStorage.FileExists(/共享/ ShellContent / LiveTileImagePath.jpg))
{
myIsolatedStorage.DeleteFile(/共享/ ShellContent / LiveTileImagePath.jpg);
}

IsolatedStorageFileStream copyFileStream = myIsolatedStorage.CreateFile(/共享/ ShellContent / LiveTileImagePath.jpg);
wb.SaveJpeg(copyFileStream,wb.PixelWidth,wb.PixelHeight,0,100);
copyFileStream.Close();

// UpdateTile
ShellTile瓦= ShellTile.ActiveTiles.First();

如果(瓦!= NULL)
{
FlipTileData tileData =新FlipTileData()
{
标题=标题,
backTitle =backtitle,
BackContent =,
WideBackContent =,
SmallBackgroundImage =新的URI(/图片/ SmallIconBW.png,UriKind.RelativeOrAbsolute)
=的BackgroundImage新的URI(/图片/ MediumKiss336.png,UriKind.RelativeOrAbsolute),
BackBackgroundImage =新的URI(isostore:/Shared/ShellContent/LiveTileImagePath.jpg,UriKind.RelativeOrAbsolute),
WideBackgroundImage =新的URI(/图片/ MediumKiss336.png,UriKind.RelativeOrAbsolute),
WideBackBackgroundImage =新的URI(isostore:/Shared/ShellContent/LiveTileImagePath.jpg,UriKind.RelativeOrAbsolute)
};

tile.Update(tileData);
}
}



要解决的问题



当我使用新的方法来保存图像的问题出现了。我似乎无法找到更新的瓷砖正确的图像路径。下面是我保存图像(新方法注意 FILESTREAM 是包含我从一个WriteableBitmap的产生早些时候方法JPEG图像一个MemoryStream它可以节省精存储在应用程序使用,而不是共享文件夹)

  StorageFile liveTileImageFile =等待localFolder.CreateFileAsync (/Shared/ShellContent/LiveTileImagePath.jpg,CreationCollisionOption.ReplaceExisting); 

fileStream.Seek(0,SeekOrigin.Begin);使用

(流sharedFolderStream =等待liveTileImageFile.OpenStreamForWriteAsync())
{
等待fileStream.CopyToAsync(sharedFolderStream);
}

ShellTile瓦= ShellTile.ActiveTiles.First();

如果(瓦!= NULL)
{
FlipTileData tileData =新FlipTileData()
{
标题=吻词典,
BackTitle =名,
BackContent =,
WideBackContent =,
SmallBackgroundImage =新的URI(/图片/ SmallIconBW.png,UriKind.RelativeOrAbsolute),
的BackgroundImage =新的URI(/图片/ MediumKiss336.png,UriKind.RelativeOrAbsolute),
BackBackgroundImage =新的URI(isostore:/Shared/ShellContent/LiveTileImagePath.jpg,UriKind.RelativeOrAbsolute),
WideBackgroundImage =新的URI(/图片/ MediumKiss336.png,UriKind.RelativeOrAbsolute),
WideBackBackgroundImage =新的URI(isostore:/Shared/ShellContent/LiveTileImagePath.jpg,UriKind.RelativeOrAbsolute)
};

tile.Update(tileData);
}



现在的问题是,我不能将图像保存到共享/ ShellFolder,我得到这个异常消息:



文件名,目录名或卷标语法不正确。 (从HRESULT异常:0x8007007B)



如果您已更新您的应用程序的LiveTile更新,请解释一下你用来保存路径和您分配给文件路径。您LiveTile图片



更新:



罗布的回答以下修复了异常,并让我保存的文件夹。我做的改变对我的瓷砖更新中使用的文件路径(感谢Scott洛夫格罗夫的建议):



而不是旧的方式:

  BackBackgroundImage =新的URI(isostore:/Shared/ShellContent/LiveTileImagePath.jpg,UriKind.RelativeOrAbsolute)

我用这个成功

  BackBackgroundImage =新的URI( isostore:shared\\shellcontent\\LiveTileImagePath.jpg,UriKind.RelativeOrAbsolute)


解决方案

这是丑陋的,但它的工作原理...你需要深入到独立存储的每个目录。你不能只是把完整路径(这将是太容易了)



因此,而不是:

  StorageFile liveTileImageFile =等待localFolder.CreateFileAsync(/共享/ ShellContent / LiveTileImagePath.jpg,CreationCollisionOption.ReplaceExisting); 

您应该有:

  VAR sharedFolder =等待localFolder.GetFolderAsync(共享); 
VAR shellContentFolder =等待sharedFolder.GetFolderAsync(ShellContent);
VAR liveTileImageFile =等待shellContentFolder.CreateFileAsync(LiveTileImagePath.jpg,CreationCollisionOption.ReplaceExisting);



希望有所帮助。


I am in the process of updating my Windows Phone apps to use the new WP8 file storage APIs (LocalFolder) instead of WP7 APIs (IsolatedStorageFile).

Old Working Method

Here is how I successfully save the Image to the /Shared/ShellContent folder using the IsolatedStorageFile method (notice the /Shared/ShellContent/ prefix when saving the image, and also the isostore:/Shared/ShellContent/ prefix when updating the live tile.

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
     if (myIsolatedStorage.FileExists("/Shared/ShellContent/LiveTileImagePath.jpg"))
     {
          myIsolatedStorage.DeleteFile("/Shared/ShellContent/LiveTileImagePath.jpg");
     }

     IsolatedStorageFileStream copyFileStream = myIsolatedStorage.CreateFile("/Shared/ShellContent/LiveTileImagePath.jpg");
     wb.SaveJpeg(copyFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
     copyFileStream.Close();

     //UpdateTile
     ShellTile tile = ShellTile.ActiveTiles.First();

     if (tile != null)
     {
            FlipTileData tileData = new FlipTileData()
            {
                Title = "title",
                BackTitle = "backtitle",
                BackContent = "",
                WideBackContent = "",
                SmallBackgroundImage = new Uri("/Images/SmallIconBW.png", UriKind.RelativeOrAbsolute),
                BackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
                BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute),
                WideBackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
                WideBackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)
                };

                tile.Update(tileData);
            }
}

Problem To Be Solved

The problem arises when I use the new method to save the image. I can't seem to find the right image path to update the tile with. Here is my new method to save the image (NOTE: fileStream is a MemoryStream containing the jpeg image that I generate from a WriteableBitmap earlier in the method. It saves fine to storage for use in the app, but not to the Shared folder)

StorageFile liveTileImageFile = await localFolder.CreateFileAsync("/Shared/ShellContent/LiveTileImagePath.jpg", CreationCollisionOption.ReplaceExisting);

fileStream.Seek(0, SeekOrigin.Begin);

using(Stream sharedFolderStream = await liveTileImageFile.OpenStreamForWriteAsync())
{
    await fileStream.CopyToAsync(sharedFolderStream);
}

ShellTile tile = ShellTile.ActiveTiles.First();

if (tile != null)
{
    FlipTileData tileData = new FlipTileData()
    {
        Title = "Kiss Dictionary",
        BackTitle = "name",
        BackContent = "",
        WideBackContent = "",
        SmallBackgroundImage = new Uri("/Images/SmallIconBW.png", UriKind.RelativeOrAbsolute),
        BackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
        BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute),
            WideBackgroundImage = new Uri("/Images/MediumKiss336.png", UriKind.RelativeOrAbsolute),
            WideBackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)
         };

         tile.Update(tileData);
}

The problem is that I can not save the image to the Shared/ShellFolder, I get this exception message:

The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)

If you have updated your app's LiveTile updating, please explain the path you used to save and the file path you assigned to your LiveTile images.

UPDATE:

Rob's answer below fixes the exception and allows me to save to the folder. I made an alteration to the file path used on my tile update (thanks to Scott Lovegrove's suggestion):

Instead of the old way:

BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)

I used this successfully

BackBackgroundImage = new Uri("isostore:shared\\shellcontent\\LiveTileImagePath.jpg", UriKind.RelativeOrAbsolute)

解决方案

This is ugly but it works... you need to drill down into each directory of isolated storage. You can't just put in the full path (that would be too easy).

So instead of:

StorageFile liveTileImageFile = await localFolder.CreateFileAsync("/Shared/ShellContent/LiveTileImagePath.jpg", CreationCollisionOption.ReplaceExisting);

You should have:

var sharedFolder = await localFolder.GetFolderAsync("Shared");
var shellContentFolder = await sharedFolder.GetFolderAsync("ShellContent");
var liveTileImageFile = await shellContentFolder.CreateFileAsync("LiveTileImagePath.jpg", CreationCollisionOption.ReplaceExisting);

Hope that helps.

这篇关于如何获得图片路径保存到WP8本地文件夹活瓷砖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆