访问图片库中的 creating/deleting 文件?
Accessing and creating/deleting files in Pictures Library?
我正在尝试在图片库中创建文件,如 Microsofts UWP api 中所述。
图片库通常具有以下路径。
%USERPROFILE%\图片
我在应用程序清单中启用了图片库功能。
像这样:
File.WriteAllBytes("%USERPROFILE%/Pictures", fileData);
它returns:
DirectoryNotFoundException: Could not find a part of the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\%USERPROFILE%\Pictures'
`
当我尝试使用硬编码的用户名时,如下所示:
File.WriteAllBytes("jtth.jh/Pictures", fileData);
它 returns 相同的 DirectoryNotFound 异常:
DirectoryNotFoundException: Could not find a part of the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\jtth.jh\Pictures'`
那么,如何访问图片库并向其中写入文件?很明显,我一定是遗漏了一些小东西,因为它试图访问图片库的路径在这里看起来很奇怪。
我看到它说如何 'Get the Pictures library.' 使用 StorageFolder:
public static StorageFolder PicturesLibrary { get; }
但我不确定如何将文件添加到此文件夹变量,就像我使用文件写入的方式一样。
我可以按照我尝试的方式来做吗,还是我需要跳过 StorageFolder and/or 异步循环?
说明示例:
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;
}
这是因为 UWP 应用程序在隔离存储下工作。正如您链接的 MSDN 文章中的第一个示例所说,执行如下操作:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
StorageFile file = await storageFolder.CreateFileAsync("sample.png", CreationCollisionOption.ReplaceExisting);
// Do something with the new file.
普通 File.Xxx() 调用将被隔离到您的应用程序自己的小世界。
I'm checking the Photos app on the Hololens after running through this code and it doesn't seem to be creating the picture?
如果你想在 Hololens 上的照片应用中显示你的照片,实际上你可能需要将照片保存到 CameraRoll
文件夹中。
不过好像不能直接保存,可能需要移动图片文件作为解决方法。
var cameraRollFolder = Windows.Storage.KnownFolders.CameraRoll.Path;
File.Move(_filePath, Path.Combine(cameraRollFolder, _filename));
详情请参考此。
我正在尝试在图片库中创建文件,如 Microsofts UWP api 中所述。
图片库通常具有以下路径。
%USERPROFILE%\图片
我在应用程序清单中启用了图片库功能。
像这样:
File.WriteAllBytes("%USERPROFILE%/Pictures", fileData);
它returns:
DirectoryNotFoundException: Could not find a part of the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\%USERPROFILE%\Pictures' `
当我尝试使用硬编码的用户名时,如下所示:
File.WriteAllBytes("jtth.jh/Pictures", fileData);
它 returns 相同的 DirectoryNotFound 异常:
DirectoryNotFoundException: Could not find a part of the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\jtth.jh\Pictures'`
那么,如何访问图片库并向其中写入文件?很明显,我一定是遗漏了一些小东西,因为它试图访问图片库的路径在这里看起来很奇怪。
我看到它说如何 'Get the Pictures library.' 使用 StorageFolder:
public static StorageFolder PicturesLibrary { get; }
但我不确定如何将文件添加到此文件夹变量,就像我使用文件写入的方式一样。
我可以按照我尝试的方式来做吗,还是我需要跳过 StorageFolder and/or 异步循环?
说明示例:
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;
}
这是因为 UWP 应用程序在隔离存储下工作。正如您链接的 MSDN 文章中的第一个示例所说,执行如下操作:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
StorageFile file = await storageFolder.CreateFileAsync("sample.png", CreationCollisionOption.ReplaceExisting);
// Do something with the new file.
普通 File.Xxx() 调用将被隔离到您的应用程序自己的小世界。
I'm checking the Photos app on the Hololens after running through this code and it doesn't seem to be creating the picture?
如果你想在 Hololens 上的照片应用中显示你的照片,实际上你可能需要将照片保存到 CameraRoll
文件夹中。
不过好像不能直接保存,可能需要移动图片文件作为解决方法。
var cameraRollFolder = Windows.Storage.KnownFolders.CameraRoll.Path;
File.Move(_filePath, Path.Combine(cameraRollFolder, _filename));
详情请参考此