开发者

WP7 read file from phone

I am building an app on Windows Phone 7 to transfer photos on wifi.

Firs the user select photos (that works fine) & I store paths in variable that I can use later.

When I am trying to send photo, I've got null on my stream like shown below: To be specific, I launch the next photo transfer when the progress bar has changed, so threads are not concurrent.

public transferPage()
{
  InitializeComponent();

  // Get smartphone unique ID
  object DeviceUniqueID;
  byte[] DeviceIDbyte = null;
  if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out DeviceUniqueID))
      DeviceIDbyte = (byte[])DeviceUniqueID;
   deviceId = Convert.ToBase64String(DeviceIDbyte);
   nbPhotos = photoContainer.photosUri.Count;
   photoContainer.index = 0;
   index = 0;

   transferPhoto();
 }

 private void ProgressBarValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
 {
   Thread.Sleep(3000);
   if (photoContainer.photosUri.Count > 0)
     transferPhoto();
 }

 private void transferPhoto()
 {
    string url = String.Format(photoContainer.urlTransfer, nbPhotos, deviceId);

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
 }

 private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
 {
   HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;

   // End the stream request operation
   Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
   var fs = Application.GetResourceStream(new Uri(photoContainer.photosUri[开发者_如何转开发0], UriKind.RelativeOrAbsolute)).Stream;

A is Null exception is raised on the last line. It is never raised on the first file, sometimes the second and always the third. photoContainer.photosUri[0] still a good file path which I confirmed in the debugger.


I'm confused. If you're uploading pictures why are you using Application.GetResourceStream? That method only accesses resources embedded within the application XAP. Shouldn't you be using MediaLibrary.Pictures to access pictures on the phone instead?

Either way, Application.GetResourceStream returns null when given an unknown resource identifier. Spend some time getting familiar with Silverlight's relative Uris, how they relate to the files' build action and how to properly formulate them. For an embedded resource the relative Uri is "/assemblyName;component/folderInProject/fileName.fileExtension" and for "Build Action = Content" file it'll be "folerInProject/fileName.fileExtension".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜