[英]Accessing and creating/deleting files in Pictures Library?
我試圖按照Microsoft 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;
}
這是因為UWP應用程序在隔離存儲下工作。 就像鏈接的MSDN文章中的第一個示例所說,請執行以下操作:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
StorageFile file = await storageFolder.CreateFileAsync("sample.png", CreationCollisionOption.ReplaceExisting);
// Do something with the new file.
普通File.Xxx()調用將隔離到應用程序自己的小世界。
經過這段代碼后,我正在Hololens上檢查“照片”應用程序,它似乎不是在創建圖片嗎?
如果要在Hololens的“照片”應用程序中顯示圖片,則實際上可能需要將圖片保存到CameraRoll
文件夾中。
但是似乎無法直接保存它,您可能需要移動圖片文件作為解決方法。
var cameraRollFolder = Windows.Storage.KnownFolders.CameraRoll.Path;
File.Move(_filePath, Path.Combine(cameraRollFolder, _filename));
詳細信息請參考該類似線程 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.