locked
Trying to use the file picker, not sure if it works as it should

    Question

  • Hi! I want to use the file picker to let the user choose an image from the hard drive that is going to be the background in the app. Therefore, I need to have access to the image so it is still the background next time the user launches the app.

    I tried to follow a sample, and everything works when I click “suspend and shutdown” to simulate the app being terminated from visual studio. But when I launch the app and try to terminate it manually (dragging the app down with the mouse) I do not have access anymore, at least the image does not appear. So I just wondered if this is normal, or if my application does not work as it should?

     

    I don't know if it is necessary, but here is the code in the home.js that I ended up with:
    (function () {
        "use strict";
    
        var photoObject = {
    
            src: null,
        };
    
        var homePage = WinJS.UI.Pages.define("/pages/home/home.html", {
            // This function is called whenever a user navigates to this page. It
            // populates the page elements with the app's data.
            ready: function (element, options) {
                // TODO: Initialize the page here.
    
                document.getElementById('Upload').addEventListener("click", this.UploadClick, false);
    
                if (Windows.Storage.ApplicationData.current.roamingSettings.values["previousExecutionState"] === 
    Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {
    
                    var mruToken = Windows.Storage.ApplicationData.current.roamingSettings.values["mruToken"];
    
                    if (mruToken) {
    
                        Windows.Storage.AccessCache.StorageApplicationPermissions.
    futureAccessList.getFileAsync(mruToken).done(this.loadImage, this.displayError);
    
                    }
    
                }
            },
    
         UploadClick: function (eventinfo) {
    
            if (Windows.UI.ViewManagement.ApplicationView.value != 
    Windows.UI.ViewManagement.ApplicationViewState.snapped ||
     Windows.UI.ViewManagement.ApplicationView.tryUnsnap() === true) {
    
                var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
    
                openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
    
                openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
    
                openPicker.fileTypeFilter.clear();
                openPicker.fileTypeFilter.append(".bmp");
                openPicker.fileTypeFilter.append(".jpeg");
                openPicker.fileTypeFilter.append(".png");
                openPicker.fileTypeFilter.append(".jpg");
    
                openPicker.pickSingleFileAsync().done(
                    homePage.prototype.loadImage,
                    homePage.prototype.displayError);
    
            }
    
        },
    
         loadImage: function(file) {
            
            if (file) {
    
                var imageBlob = URL.createObjectURL(file, { oneTimeOnly: true });
                photoObject.src = imageBlob;
    
                var appData = Windows.Storage.ApplicationData.current;
                var roamingSettings = appData.roamingSettings;
    
                roamingSettings.values["mruToken"] = 
    Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.add(file);
    
                var displayImage = document.getElementById("displayImage");
                WinJS.Binding.processAll(displayImage, photoObject);
    
            } 
    
        },
    
         displayError: function(error) {  
    
         }
    
        });
    })();
    Thanks for the help in advance!
    Thursday, January 02, 2014 2:56 PM

Answers

  • Hi Colombium,

    Before answering the question, I would like suggest you to read this : Part 2: Manage app lifecycle and state

    oncheckpoint eventis the one that you need to use, this event handler gets called when Windows is about to suspend our app. You could save your mruToken while the oncheckpoint event fired.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Colombium Friday, January 03, 2014 11:48 AM
    Friday, January 03, 2014 2:33 AM
    Moderator

All replies

  • Hi Colombium,

    Before answering the question, I would like suggest you to read this : Part 2: Manage app lifecycle and state

    oncheckpoint eventis the one that you need to use, this event handler gets called when Windows is about to suspend our app. You could save your mruToken while the oncheckpoint event fired.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Colombium Friday, January 03, 2014 11:48 AM
    Friday, January 03, 2014 2:33 AM
    Moderator
  • Hi James!
    Thanks a lot, I now managed to save the mruToken before the app suspends and it seems everything is working as it should now.

    Thanks for the help!

    Friday, January 03, 2014 11:48 AM