locked
Let an user save an image from my app

    Question

  • I'm looking for an article about this for the last couple of hours, but unfortunately I failed finding them so I'd like to ask it here, as one of my last hopes.

    I would like the users from my app be able to save an image they are looking at by simply clicking on a custom button. I've tried multiple methods but they all failed. At the moment I've been trying to use this one: 

                Windows.Storage.KnownFolders.picturesLibrary("http://ladygadgets.blogo.nl/files/2012/07/test.jpg",
                Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
                sampleFile = file;
                });

    But it just doesn't work. I'm getting an error about I cannot use picturesLibrary as well, and I couldn't find how to declare it in the manifest.

    So my question is: How would I save "http://ladygadgets.blogo.nl/files/2012/07/test.jpg" to an user's picture library under a folder which has the name of my app. (So, I'd like http://ladygadgets.blogo.nl/files/2012/07/test.jpg to be saved in the regular pictures library, in the folder "myApp")

    Thanks!

    Wednesday, July 3, 2013 2:08 PM

Answers

  • The first argument to createFileAsync is the local filename that you're creating within the picturesLibrary folder. This should be your target name, not the source name.

    In your scenario, you really need to download the remote image into a file. To do that, use the Windows.Networking.BackgroundTransfer API. You will still need the createFileAsync code to create a StorageFile object for your local target file. Once you have that, you can pass it to BackgroundDownloader.createDownload along with a URI object (a Windows.Foundation.Uri). That will do the download directly into the file.

    There's a little more to it, but you can find exactly this code in scenario 1 of the Background Transfer sample, which saves a remote image to the picture library.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    • Marked as answer by Stefan1294 Thursday, July 4, 2013 12:30 PM
    Wednesday, July 3, 2013 5:46 PM

All replies

  • Hi,To save directly to the pictures library you have to declare in the AppManifest > Capabilities that the App requires access to the Documents Libraries, otherwise you can't save directly there.

    Wednesday, July 3, 2013 3:02 PM
  • Hey,

    Thank you. That error is gone now. However, it doesn't save the picture. It does save "text.txt" but not the image I want it to save.

    This is my current code:

                Windows.Storage.KnownFolders.picturesLibrary.createFileAsync("http://ladygadgets.blogo.nl/files/2012/07/test.jpg", Windows.Storage.CreationCollisionOption.replaceExisting).done(
                function (file) {
                    sampleFile = file;
                },
                    // Handle errors with an error function
                function (error) {
                    // Process errors
                }
                );

    • Edited by Stefan1294 Wednesday, July 3, 2013 4:23 PM Fixed the error, new problem popped up
    Wednesday, July 3, 2013 4:17 PM
  • The first argument to createFileAsync is the local filename that you're creating within the picturesLibrary folder. This should be your target name, not the source name.

    In your scenario, you really need to download the remote image into a file. To do that, use the Windows.Networking.BackgroundTransfer API. You will still need the createFileAsync code to create a StorageFile object for your local target file. Once you have that, you can pass it to BackgroundDownloader.createDownload along with a URI object (a Windows.Foundation.Uri). That will do the download directly into the file.

    There's a little more to it, but you can find exactly this code in scenario 1 of the Background Transfer sample, which saves a remote image to the picture library.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    • Marked as answer by Stefan1294 Thursday, July 4, 2013 12:30 PM
    Wednesday, July 3, 2013 5:46 PM
  • Thank you, it's working fine now.
    Thursday, July 4, 2013 12:34 PM