locked
"Background Transfer Service Sample" how to validate sample file downloaded RRS feed

  • Question

  • Hi,

    I am trying using sample provided by MS for windows phone 7.1 build sample named "Background Transfer Service Sample" I am trying to modify sample to display images downloaded by this sample but whenever I try to display images it displays nothing.. :-( so how can I validate the data downloaded is perfect.

    Here is a code snippit:

                List<BitmapImage> ImageList = new List<BitmapImage>();  
                IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();  
                string[] strFileNames = isoFile.GetFileNames("\\Transfers\\*");  
                if (!isoFile.DirectoryExists("Transfers"))  
                {  
                    isoFile.CreateDirectory("Transfer");  
                }  
                for (int i = 0; i < strFileNames.Length; i++)  
                {  
                    Uri uri = new Uri("/Transfers/" + strFileNames[i], UriKind.Relative);  
                    ImageList.Add(new BitmapImage(uri));  
                }  
                lstBoxDisplayImage.ItemsSource = ImageList; 

    let me know guys if I am doing somethign wron..

    Thanks and Regards
    Nishant Rana

    Tuesday, May 31, 2011 8:45 AM

Answers

  • Hi Mark,

    Thanks for your info! Definitely ObservableCollection is an better option to be used but list calss also serves. I have tried and tested in case of manual list creation and then referencing for the files which are added as a project files.

    However, I guess this issues is because of not proper path/reference provided while creating an uri and providing Uri to a new BitmapImage object.

    To varify this I added a "Image" control and then tried assigning:

    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    string[] strFileNames = isoFile.GetFileNames("file://transfers//*");
    Uri uri = new Uri("/Transfers/" + strFileNames[i], UriKind.Relative);
    ImageControl.Source = new BitmapImage(uri);

    Still I cant see Image being displayed in that ImageControl. In that sample I did wait for a file to download first and then launch this piece of code.

    So is it a right way to provide a path for downloaded file (i.e "/Transfers/" + strFileNames[i]) or should I use in some different way to gain acces to that "transfer" folder for all downloaded files.

    Thanks and Regards
    Nishant Rana
    Wednesday, June 1, 2011 5:50 AM

  • IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    string[] strFileNames = isoFile.GetFileNames("file://transfers//*");
    Uri uri = new Uri("/Transfers/" + strFileNames[i], UriKind.Relative);
    ImageControl.Source = new BitmapImage(uri);
    BitmapImage will interpret that path as pointing to an image file in your project, not Isolated Storage.

    You need to open the file in Isolated Storage, and use the file stream with BitmapImage.
    This example demonstrates you what you need to do: isolated storage : working with files (scroll down to the "working with files" section)
    Wednesday, June 1, 2011 7:20 AM
  • Thanks mSpot,

    Yeah your post was helpful. Initially i thought project file also saves under Isolated Storage which mean project file storage is same as IsolatedStorage.

    Well my doubt is clear now. So, here is the solution for the problem:

     

    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    for (int i = 0; i < strFileNames.Length; i++)
    {
    BitmapImage bitmap = new BitmapImage();
    using(IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream("/transfers/" + strFileNames[i], FileMode.Open, isoFile))
    {
        bitmap.SetSource(isoFileStream);
        ImageList.Add(bitmap);
        isoFileStream.Close();
    }
    }
    lstBoxDisplayImage.ItemsSource = ImageList;

    Well, Yes not forget to use ObservableCollection insted of list class since that is helpfull in other scenarios.

    Thanks and Regards
    Nishant Rana

     

    Wednesday, June 1, 2011 10:38 AM

All replies

  • Hi Nishant,

    ItemsSource needs to be referencing an ObservableCollection (not the List class) for binding to successfully occur.  There are many samples on the web, here is one:

    http://msdn.microsoft.com/en-us/library/cc265158(v=VS.95).aspx

    Hope this helps,
    Mark
    Tuesday, May 31, 2011 6:00 PM
  • Hi Mark,

    Thanks for your info! Definitely ObservableCollection is an better option to be used but list calss also serves. I have tried and tested in case of manual list creation and then referencing for the files which are added as a project files.

    However, I guess this issues is because of not proper path/reference provided while creating an uri and providing Uri to a new BitmapImage object.

    To varify this I added a "Image" control and then tried assigning:

    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    string[] strFileNames = isoFile.GetFileNames("file://transfers//*");
    Uri uri = new Uri("/Transfers/" + strFileNames[i], UriKind.Relative);
    ImageControl.Source = new BitmapImage(uri);

    Still I cant see Image being displayed in that ImageControl. In that sample I did wait for a file to download first and then launch this piece of code.

    So is it a right way to provide a path for downloaded file (i.e "/Transfers/" + strFileNames[i]) or should I use in some different way to gain acces to that "transfer" folder for all downloaded files.

    Thanks and Regards
    Nishant Rana
    Wednesday, June 1, 2011 5:50 AM

  • IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    string[] strFileNames = isoFile.GetFileNames("file://transfers//*");
    Uri uri = new Uri("/Transfers/" + strFileNames[i], UriKind.Relative);
    ImageControl.Source = new BitmapImage(uri);
    BitmapImage will interpret that path as pointing to an image file in your project, not Isolated Storage.

    You need to open the file in Isolated Storage, and use the file stream with BitmapImage.
    This example demonstrates you what you need to do: isolated storage : working with files (scroll down to the "working with files" section)
    Wednesday, June 1, 2011 7:20 AM
  • Thanks mSpot,

    Yeah your post was helpful. Initially i thought project file also saves under Isolated Storage which mean project file storage is same as IsolatedStorage.

    Well my doubt is clear now. So, here is the solution for the problem:

     

    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    for (int i = 0; i < strFileNames.Length; i++)
    {
    BitmapImage bitmap = new BitmapImage();
    using(IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream("/transfers/" + strFileNames[i], FileMode.Open, isoFile))
    {
        bitmap.SetSource(isoFileStream);
        ImageList.Add(bitmap);
        isoFileStream.Close();
    }
    }
    lstBoxDisplayImage.ItemsSource = ImageList;

    Well, Yes not forget to use ObservableCollection insted of list class since that is helpfull in other scenarios.

    Thanks and Regards
    Nishant Rana

     

    Wednesday, June 1, 2011 10:38 AM
  • Hi Nishant,

    Your addition code is excellent. I have not yet tried the sample. But I will also add your code when I try it. Yes, if we don't display the read images, we can't confirm that we have read all correctly.

    You should publish this codelet in a blog, if you are a blog-bird!
    Wednesday, June 1, 2011 3:19 PM
  • Thanks.. ESM,

    Although, I am n ot really a kind of blog-bird. But i'll take extra pain to write it down if few other may be benifited by this.

    Will keep you all posted here.

    Thanks and Regards
    Nishant Rana
    Thursday, June 2, 2011 3:28 AM
  • Thanks mSpot,

    Yeah your post was helpful. Initially i thought project file also saves under Isolated Storage which mean project file storage is same as IsolatedStorage.

    Well my doubt is clear now. So, here is the solution for the problem:

     

    IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication();
    for (int i = 0; i < strFileNames.Length; i++)
    {
    BitmapImage bitmap = new BitmapImage();
    using(IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream("/transfers/" + strFileNames[i], FileMode.Open, isoFile))
    {
        bitmap.SetSource(isoFileStream);
        ImageList.Add(bitmap);
        isoFileStream.Close();
    }
    }
    lstBoxDisplayImage.ItemsSource = ImageList;

    Well, Yes not forget to use ObservableCollection insted of list class since that is helpfull in other scenarios.

    Thanks and Regards
    Nishant Rana

     

     



    Hi Nishant,

    Can you provide the xaml code that you used to define lstBoxDisplayImage ListBox?
    Friday, June 3, 2011 1:38 AM
  • Can you provide the xaml code that you used to define lstBoxDisplayImage ListBox?


    Dear EMS,

    I have update the entire project @CodePlex Site You can find the Project here and Code what you are looking for is here

    Thanks and Regards
    Nishant Rana
    Friday, June 3, 2011 3:58 AM
  • Thanks, Nishant!
    Friday, June 3, 2011 2:40 PM
  • Your welcome. :)
    Monday, June 13, 2011 7:54 AM