locked
Using the share charm to share an local image

    Question

  • Hi All,

    We're capturing a screenshot of our end of game screen to local storage and we're trying to share this using the share charm.

    1. // share image
    2. Windows::ApplicationModel::Package^ package = Windows::ApplicationModel::Package::Current;
    3. Windows::Storage::StorageFolder^storageFolder =getWritableFolder(false);
    4. autotest = Windows::Storage::StorageFile::GetFileFromPathAsync(storageFolder->Path + "\\Screenshots\\capture.png");
    5. test->Completed = ref new AsyncOperationCompletedHandler<Windows::Storage::StorageFile^>(
    6. [=] (IAsyncOperation<Windows::Storage::StorageFile^>^pAsyncOp, AsyncStatusstatus)
    7. {
    8. Windows::Storage::Streams::RandomAccessStreamReference^streamReference;
    9. switch (status)
    10. {
    11. case AsyncStatus::Completed:
    12. streamReference = Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(pAsyncOp->GetResults());
    13. request->Data->SetBitmap(streamReference);
    14. printf("Set the bitmap for sharing\n");
    15. printf("AsyncStatus Completed\n");
    16. break;
    17. case AsyncStatus::Started:
    18. printf("AsyncStatus Started\n");
    19. break;
    20. case AsyncStatus::Canceled:
    21. printf("AsyncStatus Canceled\n");
    22. break;
    23. case AsyncStatus::Error:
    24. wprintf(L"GetFileAsync - Failed with error code: 0x%0.8x\n",pAsyncOp->ErrorCode.Value);
    25. break;
    26. default:
    27. break;
    28. }
    29. });


    We've tried various permutations of the path, to no avail. It consistently fails. Is there something we're missing? Is it possible to share an image from this path?

    Thanks, David Goemans

    Friday, April 13, 2012 8:08 AM

Answers

  • Since it fails at the first async operation I'm guessing that the problem is that the problem is that you haven't set the data before the DataRequested event returns. By default we assume that everything is done by that time. If you can't finish by then (because you're calling something async) then you can request a deferral from the DataRequest, do your async stuff, and then Complete the deferral when you're done. Another option would be to remember where your bitmap is when you save it rather than getting the file anew when it's time to share. That would let you eliminate the async call form the

    Other than that and not setting the properties (Title, Description, Thumbnail) on the DataRequest this looks like it should work. You might want to compare with the ShareSource app.

    BTW: it would be a lot easier in the future if you could include your code without the line numbers. It's very hard to read here and a bit of a pain to remove them to paste into VS. It would also help to include the full function. I assume that this is your DataRequested event handler and that the Deferral is missing from the actual function and not just from this snippet, but it is hard to diagnose problems without context.

    --Rob

    • Marked as answer by David Goemans Friday, April 27, 2012 1:23 PM
    Friday, April 20, 2012 1:49 AM
    Owner

All replies

  • What does getWriteableFolder return? What is that path you are trying to open? Where does this fail?

    --Rob

    Saturday, April 14, 2012 1:01 AM
    Owner
  • getWriteableFolder just returns the current local storage folder - %userhome%\AppData\Local\Packages\%buildid%\LocalState\

    We use this all over our game for save files and such, so it's not really an issue. The png file is also in the specified path. The error we're getting back is non-description, and the failure happens in the first ASync operation. Is it possible to load a file by these means to the share charm?

    Monday, April 16, 2012 7:50 AM
  • Since it fails at the first async operation I'm guessing that the problem is that the problem is that you haven't set the data before the DataRequested event returns. By default we assume that everything is done by that time. If you can't finish by then (because you're calling something async) then you can request a deferral from the DataRequest, do your async stuff, and then Complete the deferral when you're done. Another option would be to remember where your bitmap is when you save it rather than getting the file anew when it's time to share. That would let you eliminate the async call form the

    Other than that and not setting the properties (Title, Description, Thumbnail) on the DataRequest this looks like it should work. You might want to compare with the ShareSource app.

    BTW: it would be a lot easier in the future if you could include your code without the line numbers. It's very hard to read here and a bit of a pain to remove them to paste into VS. It would also help to include the full function. I assume that this is your DataRequested event handler and that the Deferral is missing from the actual function and not just from this snippet, but it is hard to diagnose problems without context.

    --Rob

    • Marked as answer by David Goemans Friday, April 27, 2012 1:23 PM
    Friday, April 20, 2012 1:49 AM
    Owner