【问题标题】:Accessing and creating/deleting files in Pictures Library?访问和创建/删除图片库中的文件?
【发布时间】:2017-08-01 17:21:19
【问题描述】:

我正在尝试在图片库中创建一个文件,如 Microsofts 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;
}

【问题讨论】:

  • 当您读取图像时,您应该能够读取文件名,因此当您根据@TanveerBadar 的回答创建存储文件时,您应该能够使用相同的文件名。
  • @AVK 更新帖子。运行此代码后,我正在检查 Hololens 上的照片应用程序,它似乎没有在这里创建图片?代码运行顺利,我可以告诉...想法?
  • 通过 StorageFile 执行此操作,应用程序没有太多权限,部分是它自己的沙箱。有些方法不起作用,尤其是那些在文件路径上工作的方法。不知何故similar question is here.

标签: c# uwp isolatedstorage hololens


【解决方案1】:

运行此代码后,我正在检查 Hololens 上的照片应用,但它似乎没有创建图片?

如果您希望您的图片在 Hololens 上的照片应用中显示,实际上您可能需要将图片保存到 CameraRoll 文件夹中。

但是好像不能直接保存,可能需要移动图片文件来解决。

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

详情请参考similar thread

【讨论】:

  • 如果是这样,那么隔离存储的意义何在?它被归类为错误还是设计?
【解决方案2】:

这是因为 UWP 应用程序在隔离存储下工作。就像您链接的 MSDN 文章中的第一个示例所说,执行以下操作:

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

普通的 File.Xxx() 调用将被隔离到您的应用程序自己的小世界中。

【讨论】:

  • 好的,但是'sample.png'是什么?我有图像被读入我的程序到一个字节数组中。我将如何将其输入到这个
  • 这是从 MSDN 逐字复制的示例。如果没有看到您迄今为止自己尝试过的事情,我无法为您提供更多帮助。
  • 更新帖子。运行此代码后,我正在检查 Hololens 上的照片应用程序,它似乎没有创建图片?
猜你喜欢
  • 2012-11-12
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多