4

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!

Bells
  • 1,465
  • 1
  • 19
  • 30
  • Have you tried sharing to other apps? Facebook might not support those data types. – Peter Torr - MSFT May 09 '15 at 23:30
  • @Peter Torr MSFT - I tried sharing to twitter, but there also the image is not appearing. One more problem I faced is that the default mail app and google+ are not displayed in the app list in the share pane. – Bells May 11 '15 at 06:12

2 Answers2

3

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.
Sender
  • 6,660
  • 12
  • 47
  • 66
  • Thank you, will check this way and update the result. – Bells May 13 '15 at 09:10
  • Please, check this out: http://stackoverflow.com/questions/42856996/uwp-share-feature-not-working-in-windows-10-mobile?noredirect=1#comment72822335_42856996 it seems it's failing somewhat related. – SuperJMN Mar 17 '17 at 13:01
-3

I think you refer to Share Target in UWP You may refer to this URL https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ShareSource

This sample demonstrates how an app shares content with another app. This sample uses classes from the Windows.ApplicationModel.DataTransfer namespace. Some of the classes you might want to review in more detail are the DataTransferManager class, which you use to initiate a share operation, and the DataPackage class, which you use to package the content. Because each share scenario usually involves two apps—the source app and a target app that receives the content—we recommend you install and deploy the Sharing content target app sample when you install and run this one. This way, you can see how sharing works from end to end.

This sample covers how to share content in a variety of formats, including:

1.Text 2.Web link 3.Application link 4.Images 5.Files 6.Delay-rendered files 7.HTML content 8.Custom data

protected override bool GetShareContent(DataRequest request)
    {
        bool succeeded = false;

        if (this.imageFile != null)
        {
            DataPackage requestData = request.Data;
            requestData.Properties.Title = TitleInputBox.Text;
            requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
            requestData.Properties.ContentSourceApplicationLink = ApplicationLink;

            // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
            // since the target app may only support one or the other.

            List<IStorageItem> imageItems = new List<IStorageItem>();
            imageItems.Add(this.imageFile);
            requestData.SetStorageItems(imageItems);

            RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile);
            requestData.Properties.Thumbnail = imageStreamRef;
            requestData.SetBitmap(imageStreamRef);
            succeeded = true;
        }
        else
        {
            request.FailWithDisplayText("Select an image you would like to share and try again.");
        }
        return succeeded;
    }
Eng Soon Cheah
  • 257
  • 1
  • 10
  • 42
  • 1
    Another copy-pasted answer from the link. It might be the 10-th I found in your answers. Please consider how to answer your questioners more properly - or rather - more personally I would say. A copy pasted answer is only worth for comments. – Ian Mar 21 '16 at 04:34
  • Update with Source Code – Eng Soon Cheah Mar 21 '16 at 04:36
  • 1
    If you do copy/paste code from elsewhere, you **must** make it clear it's not written by you. Just adding a link isn't enough. Use [quote formatting](http://stackoverflow.com/help/formatting) (start the line with a `>`) to distinguish your words from the words you copy. – Wai Ha Lee Mar 21 '16 at 04:46