简体   繁体   中英

Accessing and creating/deleting files in Pictures Library?

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;
}

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM