简体   繁体   中英

FileStream to open an image “System.UnauthorizedAccessException” Access to the path is denied

I'm writing a wp8 app. I have a problem bothers me a few days. I want to uplaod an photo to server. I choose a photo from album and I use FileStream to upload it, but I cannot open it. It said that access to the path is denied.

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

upload

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

At the line: FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read); I encountered an error.

System.UnauthorizedAccessException: Access to the path 'C:\\Data\\Users\\Public\\Pictures\\Camera Roll\\WP_20140331_001.jpg' is denied.

I have chosen the function `D_CAP_MEDIALIB_PHOTO in WMAppMainfest.xml

I dont think you can access the Camera Roll like that. You may have to user MediaLibrary class for the same. Also, you have the image in the PhotoChooserTask_Completed event handler. You don't have to get in a File Stream.

I saw that u solved ur problem by urself still no solving code here i hope someone will find use for it :)

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

You also can delete the (new) picture instantly ! (if you want)

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