2017-07-31 32 views
0

我試圖在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; 
} 
+0

當你讀取圖像時,你應該能夠讀取文件名,所以當你根據@TanveerBadar的答案創建存儲文件時,你應該能夠使用相同的文件名。 – AVK

+0

@AVK更新後。我在運行此代碼後檢查Hololens上的照片應用程序,但似乎並未在此創建照片?代碼運行順暢,從我可以告訴...想法? – jtth

+0

通過* StorageFile *,通過它自己的沙箱,應用程序沒有太多特權。某些方法不起作用,尤其是那些處理文件路徑的方法。不知何故[類似的問題在這裏](https://stackoverflow.com/q/41870125/2681948)。 – Romasz

回答

1

我在運行此代碼後檢查Hololens上的照片應用程序,它似乎沒有創建圖片?

如果您希望在Hololens的照片應用中顯示您的照片,實際上您可能需要將照片保存到CameraRoll文件夾中。

但它似乎不能直接保存,您可能需要移動圖片文件作爲解決方法。

var cameraRollFolder = Windows.Storage.KnownFolders.CameraRoll.Path;    
File.Move(_filePath, Path.Combine(cameraRollFolder, _filename)); 

詳情請參考此similar thread

+0

如果是這樣,那麼隔離存儲有什麼意義?它被分類爲錯誤還是設計? –

1

這是因爲應用程序UWP下獨立存儲工作。就像你的鏈接MSDN文章中的第一個例子說,做這樣的事情:

StorageFolder storageFolder = KnownFolders.PicturesLibrary; 
StorageFile file = await storageFolder.CreateFileAsync("sample.png", CreationCollisionOption.ReplaceExisting); 
// Do something with the new file. 

平原File.Xxx()調用將被隔離到應用程序的自己的小世界。

+0

好的,但'sample.png'是什麼?我有我的程序讀入一個字節數組的圖像。我將如何將此內容添加到此 – jtth

+0

這是從MSDN逐字複製的示例。沒有看到你自己嘗試過的東西,我無法幫助你。 –

+0

更新後。在運行此代碼後,我正在檢查Hololens上的照片應用程序,但似乎沒有創建該照片? – jtth