user1155478
user1155478

Reputation: 23

How to store image files in the isolated storage before the application opens?

I want to store jpeg files in the isolated storage before the application starts executing.I have set these images as the source for an image control (via data binding)in the first page of my application.But each time the application executes, i get an isolated storage exception.I have inserted the following code in the application_launchin() function in App.xaml.cs.I also tried inserting it in the constructor of App.xaml.cs but still got the exception.I have used data binding for the first page and an exception is raised in the converter.please help

 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                    myIsolatedStorage.CreateDirectory("DirForMyImage");
                    bi = new BitmapImage(new Uri("/images/myImage.jpg", UriKind.Relative));
                    bi.ImageOpened += new EventHandler<RoutedEventArgs>(imageTest_ImageOpened);}

This is the event handler where I am saving the images

 void imageTest_ImageOpened(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile("Certificate/Birth Certificate.jpg"))
            {

                WriteableBitmap wb = new WriteableBitmap(bi);
                wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            }}}

converter function is as follows.

public class IsoImageConverter : IValueConverter
    {
          WriteableBitmap bitmap = new WriteableBitmap(200, 200);
        //Convert Data to Image when Loading Data    
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            String path = (String)value;
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(path, FileMode.Open, FileAccess.Read))//=====>exception occurs here
            {
                // Decode the JPEG stream.
               bitmap = new WriteableBitmap(400,400);
               bitmap.LoadJpeg(fileStream);
                fileStream.Dispose();

            }


        }

        return bitmap; 

    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}  

Upvotes: 1

Views: 690

Answers (1)

ColinE
ColinE

Reputation: 70122

I would recommend placing this logic in your handler for the application Launching event. When you create a new WP7 application via the Visual Studio template, you will find the following method in your App.xaml.cs file:

  // Code to execute when the application is launching (eg, from Start)
  // This code will not execute when the application is reactivated
  private void Application_Launching(object sender, LaunchingEventArgs e)
  {
  }

This method is called when the Launching event fires.

Upvotes: 1

Related Questions