Search code examples
c#facebookwindows-10fileshare

Unable to share image using DataTransferManager in Windows 10


Requirement: Share a text and image using DataTransferManager into Facebook in Windows 10.

Problem: Unable to share image.

Below shown is the code I used,

 private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequestDeferral deferral = args.Request.GetDeferral();
            args.Request.Data.Properties.Title = "Sharing sample";
            args.Request.Data.SetText("Testing share in universal app");
            var imageUri = "http://cdn.vrworld.com/wp-content/uploads/2015/01/microsoft-announces-windows-10_ahab.1920.jpg";

            //var storageFile = await StorageFile.CreateStreamedFileFromUriAsync("ShareFile", new Uri(imageUri), null);
            //List<IStorageItem> storageItems = new List<IStorageItem>();
            //storageItems.Add(storageFile);
            //args.Request.Data.SetStorageItems(storageItems);

            args.Request.Data.SetBitmap(Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri(imageUri)));
            deferral.Complete();
        }

When I use SetBitmap method, only the title and text are being shared. The image is neither displayed in the share panel nor shared to the target app.

When I use SetStorageItems (see commented code), none of the items are shared. The default "What's on your mind" text appears on the share panel.

Any feedback is appreciated, thank you!


Solution

  • Sharing of URI streamed files is unfortunately not supported. Here's how I would go about doing this:

    1. When the user clicks the share button, start downloading the file and show some sort of progress if it's a big file. You could also pre-download the file of course. Set up a StorageFile instance containing the file.
    2. Call DataTransferManager.ShowShareUI
    3. In your DataRequested handler, use SetStorageItems to share the StorageFile instance.