Search code examples
c#windows-phone-8isolatedstorage

How to open and split the most recent image from isolated storage (WP8)?


I am currently developing an app that would do the following tasks:

1- taking a picture and saving it in both : Isolated storage and the photo gallery.

2-then loading the picture in a different page while being split into different sections.

so I followed this tutorial on how to capture a photo and save it:

http://msdn.microsoft.com/en-us/library/windows/apps/hh202956(v=vs.105).aspx

now I am stuck on the part of viewing and splitting the most recent image in a different page.

Any solutions or ideas?


Solution

  • Sorry for not being clear about that, all I want is a way to view the most recent photo captured by my app

    The sample Camera app saves the photo to the camera roll as well as the application isolated storage. Here's the code snippet they used.

    // Save photo as JPEG to the local folder.
    using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
        {
            // Initialize the buffer for 4KB disk pages.
            byte[] readBuffer = new byte[4096];
            int bytesRead = -1;
    
            // Copy the image to the local folder. 
            while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
            {
                targetStream.Write(readBuffer, 0, bytesRead);
            }
        }
    }
    

    As you can see they save it with the file name fileName, so all you have to do is keep a List<string> of all the fileName that you used. Each time you save a new image, you want to add that fileName to the List.

    You can saved the list by using ApplicationSettings

    if (!IsolatedStorageSettings.ApplicationSettings.Contains("recent_images"))
    {
        IsolatedStorageSettings.ApplicationSettings.Add("recent_images", YOUR_LIST);        
    }
    IsolatedStorageSettings.ApplicationSettings.Save();    
    

    That way next time you load the app you can get the List again (so basically you tombstoned the List)

    List<string> recent_images = (List<string>) IsolatedStorageSettings.ApplicationSettings["recent_images"];
    

    Now to load your recent images

    <!-- create the container in xaml -->
    <Image x:Name="myImage"></Image>
    
    
    // this function loads an image from isolated storage and returns a bitmap
    private static BitmapImage GetImageFromIsolatedStorage(string imageName)
    {
        var bimg = new BitmapImage();
        using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var stream = iso.OpenFile(imageName, FileMode.Open, FileAccess.Read))
            {
                bimg.SetSource(stream);
            }
        }
        return bimg;
    }
    
    
    // load the first image in recent images
    BitmapImage first = GetImageFromIsolatedStorage(recent_images[0]);
    
    // set the BitmapImage as the source of myImage to display it on the screen
    this.myImage.Source = first;
    

    Just read the their code and my code line by line. It's not too hard if you don't skip steps.