locked
How do I transfer an image from my app to the local machine?

    Question

  • Hi there,

    I'm making an app that includes an image as it's background and I was wondering if there is a way to transfer that image to the image folders on the user's computer.

    So if we say that the uri for the image is 'ms-appx:///backgrounds/1.jpg', how would I make a copy of that image into the pictures library?

    Thanks!

    Monday, July 14, 2014 1:03 AM

Answers

  • open the file with the uri you have with ;

    StorageFile.getFileFromApplicationUriAsync

    then copy the file with :

    StorageFile.copyAsync

    and as location you set KnownFolders.picturesLibrary.

    I don't have visual studio here otherwise would have given a working sample. but my js isn't fluent enough to do without testing


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Octodonk Monday, July 14, 2014 10:25 AM
    Monday, July 14, 2014 5:27 AM
  • I came up with this but the app just crashes when I call the function. What am I doing wrong? Seems to fall over when getting the folder.

    function SaveBackground() {
        var uri = new Windows.Foundation.Uri("ms-appx:///backgrounds/1.jpg")
        Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function (file) {
            var destiationFolder = Windows.Storage.KnownFolders.savedPictures;
            var desiredName = "background.jpg";
            var option = Windows.Storage.NameCollisionOption.generateUniqueName;
    
            file.copyAsync(Windows.Storage.KnownFolders.savedPictures, desiredNewName, option).done( /* Your success and error handlers */);
        });
    }


    • Edited by Octodonk Monday, July 14, 2014 5:55 AM
    • Marked as answer by Octodonk Monday, July 14, 2014 10:25 AM
    Monday, July 14, 2014 5:54 AM

All replies

  • open the file with the uri you have with ;

    StorageFile.getFileFromApplicationUriAsync

    then copy the file with :

    StorageFile.copyAsync

    and as location you set KnownFolders.picturesLibrary.

    I don't have visual studio here otherwise would have given a working sample. but my js isn't fluent enough to do without testing


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Octodonk Monday, July 14, 2014 10:25 AM
    Monday, July 14, 2014 5:27 AM
  • I came up with this but the app just crashes when I call the function. What am I doing wrong? Seems to fall over when getting the folder.

    function SaveBackground() {
        var uri = new Windows.Foundation.Uri("ms-appx:///backgrounds/1.jpg")
        Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function (file) {
            var destiationFolder = Windows.Storage.KnownFolders.savedPictures;
            var desiredName = "background.jpg";
            var option = Windows.Storage.NameCollisionOption.generateUniqueName;
    
            file.copyAsync(Windows.Storage.KnownFolders.savedPictures, desiredNewName, option).done( /* Your success and error handlers */);
        });
    }


    • Edited by Octodonk Monday, July 14, 2014 5:55 AM
    • Marked as answer by Octodonk Monday, July 14, 2014 10:25 AM
    Monday, July 14, 2014 5:54 AM
  • code looks good. did you enable PictureLibrary access in the manifest file?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, July 14, 2014 6:18 AM
  • I did now, but it still crashes. Damn, I was sure that would work.
    Monday, July 14, 2014 6:25 AM
  • I will test it out this evening. what is the exception you get now and where ?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, July 14, 2014 6:31 AM
  • I don't get any exception, the app just crashes and debugging ends. It still seems to stem from the folder.
    Monday, July 14, 2014 6:36 AM
  • Seems to be working now.. I tested it again by mistake and it worked. yay. Thanks!
    Monday, July 14, 2014 10:25 AM