【发布时间】:2017-11-15 02:47:16
【问题描述】:
我目前正在为我的学校项目开发一个 UWP 应用程序,其中一个页面允许用户为自己拍照。我按照本教程创建了该功能:CameraStarterKit
现在我将拍摄的照片存储在我桌面的图片文件夹中。但是我的项目的要求是将拍摄的图片存储在 inetpub\wwwroot 下一个名为“Photos”的文件夹中。
我真的不明白 wwwroot 或 IIS 是什么......因此,我不知道应该如何修改我的代码并将它们存储到文件夹中。
以下是我在本地桌面上存储的代码:
private async Task TakePhotoAsync()
{
idleTimer.Stop();
idleTimer.Start();
var stream = new InMemoryRandomAccessStream();
//MediaPlayer mediaPlayer = new MediaPlayer();
//mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/camera-shutter-click-03.mp3"));
//mediaPlayer.Play();
Debug.WriteLine("Taking photo...");
await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
try
{
var file = await _captureFolder.CreateFileAsync("NYPVisitPhoto.jpg", CreationCollisionOption.GenerateUniqueName);
Debug.WriteLine("Photo taken! Saving to " + file.Path);
var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(_rotationHelper.GetCameraCaptureOrientation());
await ReencodeAndSavePhotoAsync(stream, file, photoOrientation);
Debug.WriteLine("Photo saved!");
}
catch (Exception ex)
{
// File I/O errors are reported as exceptions
Debug.WriteLine("Exception when taking a photo: " + ex.ToString());
}
}
对于文件的存储:
private static async Task ReencodeAndSavePhotoAsync(IRandomAccessStream stream, StorageFile file, PhotoOrientation photoOrientation)
{
using (var inputStream = stream)
{
var decoder = await BitmapDecoder.CreateAsync(inputStream);
using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } };
await encoder.BitmapProperties.SetPropertiesAsync(properties);
await encoder.FlushAsync();
}
}
}
【问题讨论】:
-
您无法使用 UWP 执行此操作,该应用无权访问该文件夹。您可以考虑使用另一个组件来执行此操作,例如自动将拍摄的图片从图片库复制到该文件夹的脚本。
-
您好,感谢您提供的信息...但我可以知道我们无法使用 UWP 执行此操作的原因吗?所以我给我的老师写了一份报告,向他们解释我为什么能这样做。另外,我可能想尝试将图片库中的照片复制到文件夹中,但我该怎么做呢?像javascript一样的脚本?非常感谢!:)
-
或者无论如何我可以访问应用程序上的那个文件夹?
-
UWP 应用可以访问的文件夹列表:File access permissions。您可以尝试打开文件夹选择器,也可以在链接中找到。