我試圖在Microsofts UWP api中記錄的圖片庫中創建一個文件。訪問和創建/刪除圖片庫中的文件?
圖片庫通常具有以下路徑。
%USERPROFILE%\圖片
我在應用清單中啓用了圖片庫功能。
像這樣:
File.WriteAllBytes("%USERPROFILE%/Pictures", fileData);
它返回:
DirectoryNotFoundException:找不到路徑的一部分「C:\ DATA \用戶\ DefaultAccount \應用程序數據\本地\ DevelopmentFiles \ HoloViewerVS .Debug_x86.jtth.jh \%USERPROFILE%\圖片 `
當我嘗試使用我的用戶名硬編碼的,像這樣:
File.WriteAllBytes("jtth.jh/Pictures", fileData);
返回相同的DirectoryNotFound例外:
DirectoryNotFoundException:找不到路徑的一部分「C:\ DATA \用戶\ DefaultAccount \應用程序數據\本地\ 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
@AVK更新後。我在運行此代碼後檢查Hololens上的照片應用程序,但似乎並未在此創建照片?代碼運行順暢,從我可以告訴...想法? – jtth
通過* StorageFile *,通過它自己的沙箱,應用程序沒有太多特權。某些方法不起作用,尤其是那些處理文件路徑的方法。不知何故[類似的問題在這裏](https://stackoverflow.com/q/41870125/2681948)。 – Romasz