0

I am attempting to create a file in the Pictures Library as documented in Microsofts UWP api.

The Pictures Library typically has the following path.
%USERPROFILE%\Pictures

I have enabled the Pictures library capability in the app manifest.

Like so:

File.WriteAllBytes("%USERPROFILE%/Pictures", fileData);

It returns:

DirectoryNotFoundException: Could not find a part of the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\%USERPROFILE%\Pictures' `

When I try using my username hard-coded, like so:

File.WriteAllBytes("jtth.jh/Pictures", fileData);

it returns the same DirectoryNotFound exception:

DirectoryNotFoundException: Could not find a part of the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloViewerVS.Debug_x86.jtth.jh\jtth.jh\Pictures'`

So, how do you access the Pictures Library and write files to it? Clearly I must be missing something small here as the path its trying to access the pictures library at seems quite odd here.

I see it says how to 'Get the Pictures library.' using a StorageFolder:

public static StorageFolder PicturesLibrary { get; }

But I'm not sure how to add files to this folder variable like the way I'm doing it using the file write.

Can I do it the way I am trying to do it, or do I need to jump through StorageFolder and/or async hoops?

Example for clarification:

        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;

Function:

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;
}
jtth
  • 876
  • 1
  • 12
  • 40
  • When you read images, you should be able to read the file name, so when you create your Storage File as per the answer from @TanveerBadar, you should be able to use the same file name. – AVK Jul 31 '17 at 18:09
  • @AVK Updated post. I'm checking the Photos app on the Hololens after running through this code and it doesn't seem to be creating the picture here? The code runs smoothly from what I can tell... ideas? – jtth Jul 31 '17 at 18:25
  • DO it via *StorageFile*, apps doesn't have much privileges part it's own sandbox. Some methods won't work, especially those working on filepaths. Somehow [similar question is here](https://stackoverflow.com/q/41870125/2681948). – Romasz Aug 01 '17 at 05:38

2 Answers2

1

It is because UWP applications work under isolated storage. Like the first example in your linked MSDN article says, do something like this:

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

Plain File.Xxx() calls will be isolated to your application's own little world.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • Ok, but what is 'sample.png'? I have images that are read into my program into a byte array. How would I feed that into this – jtth Jul 31 '17 at 17:46
  • That is the example copied verbatim from MSDN. I cannot help you more without seeing what you have attempted so far on your own. – Tanveer Badar Jul 31 '17 at 17:47
  • Updated post. I'm checking the Photos app on the Hololens after running through this code and it doesn't seem to be creating the picture? – jtth Jul 31 '17 at 18:26
1

I'm checking the Photos app on the Hololens after running through this code and it doesn't seem to be creating the picture?

If you want your pictures showed in the Photos app on Hololens, actually you may need to save the pictures into CameraRoll folder.

But it seems like it cannot be directly saved, you may need to move the picture file as a workaround.

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

Details please reference this similar thread.

Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21