locked
Share an image file from url

    Question

  • I want to share an image from a URL. The code is 

               imageUrl = "http://yukoart.com/wp-content/uploads/2012/05/unwritten_19.jpg";
                var deferral = request.getDeferral();
                var imageSource = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(new Windows.Foundation.Uri(imageUrl));
                imageSource.openReadAsync().done(function (randomAccessStream) {
                    request.data.properties.thumbnail = randomAccessStream;
                    //request.data.setStorageItems(randomAccessStream);
                    deferral.complete();
                });

    However, the returned attibute "randomAccessStream" is not a type of "RandomAccessStrearm". It's type of FileRandomAccessStrearm,

    So I get a error like " No such interface supported". 

    Monday, December 17, 2012 10:40 AM

Answers

  • Hi Nick,

    The object returned by "RandomAccessStreamReference.CreateFromUri" is already a RandomAccessStreamReference which you can use as the thumbnail or bitmap source. You don't need to create a file reference from it further. For example:


    function shareDataHandler(e) {
            var request = e.request;
            var deferral = request.getDeferral();
            request.data.properties.title = "Share Text Example";
            request.data.properties.description = "Demonstrates how to share text.";
            request.data.setText("Hello World!");
            // In this example, we use the imageFile for the thumbnail as well.
            var rawUrl = "http://i.s-microsoft.com/global/ImageStore/PublishingImages/logos/hp/logo-lg-1x.png";
            var urlToImage = new Windows.Foundation.Uri(rawUrl);
            request.data.properties.thumbnail = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(urlToImage);
            request.data.setBitmap(Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(urlToImage));
            deferral.complete();
        }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Nick.Mui Tuesday, December 18, 2012 3:00 AM
    Tuesday, December 18, 2012 2:42 AM
    Moderator

All replies

  • Hi Nick,

    The object returned by "RandomAccessStreamReference.CreateFromUri" is already a RandomAccessStreamReference which you can use as the thumbnail or bitmap source. You don't need to create a file reference from it further. For example:


    function shareDataHandler(e) {
            var request = e.request;
            var deferral = request.getDeferral();
            request.data.properties.title = "Share Text Example";
            request.data.properties.description = "Demonstrates how to share text.";
            request.data.setText("Hello World!");
            // In this example, we use the imageFile for the thumbnail as well.
            var rawUrl = "http://i.s-microsoft.com/global/ImageStore/PublishingImages/logos/hp/logo-lg-1x.png";
            var urlToImage = new Windows.Foundation.Uri(rawUrl);
            request.data.properties.thumbnail = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(urlToImage);
            request.data.setBitmap(Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(urlToImage));
            deferral.complete();
        }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Nick.Mui Tuesday, December 18, 2012 3:00 AM
    Tuesday, December 18, 2012 2:42 AM
    Moderator
  • Dear steven,

    Thanks a lot.

    Tuesday, December 18, 2012 3:23 AM