【发布时间】:2017-08-01 17:21:19
【问题描述】:
我正在尝试在图片库中创建一个文件,如 Microsofts UWP api 中所述。
图片库通常具有以下路径。
%USERPROFILE%\图片
我已在应用清单中启用图片库功能。
像这样:
File.WriteAllBytes("%USERPROFILE%/Pictures", fileData);
返回:
DirectoryNotFoundException:找不到路径的一部分 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\%USERPROFILE%\Pictures' `
当我尝试使用硬编码的用户名时,如下所示:
File.WriteAllBytes("jtth.jh/Pictures", fileData);
它返回相同的 DirectoryNotFound 异常:
DirectoryNotFoundException:找不到路径的一部分'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\jtth.jh\Pictures'`
那么,您如何访问图片库并向其写入文件?显然,我必须在这里遗漏一些小东西,因为它试图访问图片库的路径在这里似乎很奇怪。
我看到它说如何“获取图片库”。使用 StorageFolder:
public static StorageFolder PicturesLibrary { get; }
但我不确定如何将文件添加到此文件夹变量中,就像我使用文件写入的方式一样。
我可以按照我尝试的方式进行操作,还是需要跳过 StorageFolder 和/或异步循环?
说明示例:
string imgName = currentFolderLoaded + "/" + temp.GetComponentInChildren<Text>().text;
//example imgName to enhance clairty
imgName = C:\dev\Users\jtth\Hololens\ProjectFolder\App\HoloApp\Data\myFiles\pics\example.png
//load image
byte[] fileData;
Texture2D tex = null;
fileData = File.ReadAllBytes(imgName);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
//test hololens access
//previous attempt -> File.WriteAllBytes("%USERPROFILE%/Pictures", fileData);
//New attempt
AsStorageFile(fileData, imgName);
//Read Texture into RawImage component
ImgObject.GetComponent<RawImage>().material.mainTexture = tex;
//Enable component to render image in game
ImgObject.GetComponent<RawImage>().enabled = true;
功能:
private static async Task<StorageFile> AsStorageFile(byte[] byteArray, string fileName)
{
Windows.Storage.StorageFolder storageFolder = Windows.Storage.KnownFolders.PicturesLibrary;
Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(sampleFile, byteArray);
return sampleFile;
}
【问题讨论】:
-
当您读取图像时,您应该能够读取文件名,因此当您根据@TanveerBadar 的回答创建存储文件时,您应该能够使用相同的文件名。
-
@AVK 更新帖子。运行此代码后,我正在检查 Hololens 上的照片应用程序,它似乎没有在这里创建图片?代码运行顺利,我可以告诉...想法?
-
通过 StorageFile 执行此操作,应用程序没有太多权限,部分是它自己的沙箱。有些方法不起作用,尤其是那些在文件路径上工作的方法。不知何故similar question is here.
标签: c# uwp isolatedstorage hololens