locked
Expose picture file path as a property - KnownFolders and getFileAsync

    Question

  • I'm a WinJS newbie working on a pre-existing solution.

    The solution builds an image url based on ms-appdata:/// - whilst this works fine, we want to change it to pick up images from the Pictures folder (otherwise any app reinstall removes the images and we have to copy them in again).

    Having taken a look, I can use 'Windows.Storage.KnownFolders.picturesLibrary' and then getFolderAsync and getFileAsync.

    Ultimately returning the path of the IStorageFile as the result of the property.

    My issue is, how do I call multiple async methods and return the result exposed through a property?

    Thursday, May 2, 2013 10:15 AM

Answers

  • The way I see it you have two options.

    1) Make some function that knows how to create that url and set it as a property in your object (i don't like it that much)

    2) Make you property return a promise.

     
    var imageStatic = {
    
            _urlPrefix: "ms-appdata:///local/images",
            imageUrl: Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("sample.dat", Windows.Storage.CreationCollisionOption.replaceExisting)
    }

    and then you should use somethinf like this...

    imageStatic.imageUrl().then(function(file){
    // file is the file from the known folder
    });

    I hope I undertood your question and made myself clear.

    Regards


    http://about.me/sebagomez

    • Marked as answer by kidshaw Tuesday, May 7, 2013 1:33 PM
    Friday, May 3, 2013 2:27 PM

All replies

  • You might want to chain Promises together.

    Take a look at this link: http://msdn.microsoft.com/en-us/library/windows/apps/hh700334.aspx


    http://about.me/sebagomez

    Thursday, May 2, 2013 6:58 PM
  • I am not sure if I understand the part " result exposed through property" but if you want to get paths of all files in the pictures library, you can use this code. You can add change the filters to just get image files (PNG, JPEG etc.)

    var paths = [];
    
                var folder = Windows.Storage.KnownFolders.picturesLibrary;
    
                var options = new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.defaultQuery, ['*']);
                options.folderDepth = Windows.Storage.Search.FolderDepth.deep;
                folder.createFileQueryWithOptions(options).getFilesAsync().then(function (files) {
                    files.forEach(function (file) {
                        paths.push(file.path);
                    });
                });

    - Girija

    Thursday, May 2, 2013 9:09 PM
  • Thanks for your answers.

    Here is a snippet of the code to help explain...

    var imageStatic = {
    
            _urlPrefix: "ms-appdata:///local/images",
            imageUrl: function (productid) {var url = Kiosk.imageClass._urlPrefix + "sale" + productid + ".jpg";
                return url;
            },}


    What I want is for imageUrl to return the url <picturesfolder>/Sale/productid.jpg

    How do I rewrite this function to return the file path using Windows.Storage.KnownFolders.pictureLibrary and async calls?

    I have tried linking promises but how do I return the result of the last promise to through the imageUrl property.

    Friday, May 3, 2013 8:09 AM
  • The way I see it you have two options.

    1) Make some function that knows how to create that url and set it as a property in your object (i don't like it that much)

    2) Make you property return a promise.

     
    var imageStatic = {
    
            _urlPrefix: "ms-appdata:///local/images",
            imageUrl: Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("sample.dat", Windows.Storage.CreationCollisionOption.replaceExisting)
    }

    and then you should use somethinf like this...

    imageStatic.imageUrl().then(function(file){
    // file is the file from the known folder
    });

    I hope I undertood your question and made myself clear.

    Regards


    http://about.me/sebagomez

    • Marked as answer by kidshaw Tuesday, May 7, 2013 1:33 PM
    Friday, May 3, 2013 2:27 PM
  • That's kind of what I thought would be the case. I'm hesitant because this property is referenced several times in areas of the solution I am not familiar with so will require some effort to test and verify.

    Thanks for your ideas.


    Tuesday, May 7, 2013 1:32 PM