【问题标题】:FileStream to open an image "System.UnauthorizedAccessException" Access to the path is deniedFileStream 打开图像“System.UnauthorizedAccessException” 访问路径被拒绝
【发布时间】:2024-05-01 16:50:02
【问题描述】:

我正在编写一个 wp8 应用程序。我有一个问题困扰了我几天。 我想将照片上传到服务器。我从相册中选择了一张照片,我使用 FileStream 上传它,但我无法打开它。它说访问路径被拒绝。

PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

void photoChooserTask_Completed(object sender, PhotoResult e)
{
      if (e.TaskResult == TaskResult.OK)
      {
          // show the img
          BitmapImage bmp = new BitmapImage();
          bmp.SetSource(e.ChosenPhoto);
          ShowPhoto.Source = bmp;

          // get path of img
          string imagePath = e.OriginalFileName;
      }
}

上传

if (imagePath != null)
{
     FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
     StreamContent imageContent = new StreamContent(fs);
}

一行:FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read); 我遇到了错误。

System.UnauthorizedAccessException:对路径“C:\Data\Users\Public\Pictures\Camera Roll\WP_20140331_001.jpg”的访问被拒绝。

我在 WMAppMainfest.xml 中选择了函数 `D_CAP_MEDIALIB_PHOTO

【问题讨论】:

    标签: c# windows-phone-8


    【解决方案1】:

    我看到你自己解决了你的问题 这里仍然没有解决代码 我希望有人会发现它的用途:)

      var imageAsByteArray = File.ReadAllBytes(imagePath);
    
      // I use as example a pictureBox:
      pictureBox1.Image = byteArrayToImage(imageAsByteArray);
    
      // Or safe/copy/replace it:
      File.WriteAllBytes(picture_Path, imageAsByteArray);
    

    您也可以立即删除(新)图片! (如果你愿意)

    【讨论】:

      【解决方案2】:

      我不认为你可以像那样访问相机胶卷。您可能必须使用相同的 MediaLibrary 类。此外,您在 PhotoChooserTask_Completed 事件处理程序中有图像。您不必进入文件流。

      【讨论】:

      • 我成功解决了这个问题。我从 e.ChosenPhoto 获取流并传输到 byte[] 进行上传。谢谢!
      最近更新 更多