-1

I'm developing app for Windows Phone 8.1 which have :

Page with file open picker to take photo from gallery and second Page to sending that one photo as an e-mail attachment with message.

How i can take this one picked picture and send this with e-mail.?

I tried to look some solutions but so far without any luck.

Any suggestion please?

The code is in regular c# and xaml and I'm using Windows Phone 8.1 in Visual Studio 2015.

Eldho
  • 7,795
  • 5
  • 40
  • 77

2 Answers2

0

You can use this solution to your problem in a single step.

        /// <summary>
        /// Taking photo from the gallery
        /// </summary>
        private void SharePhotoClick(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask ph=new PhotoChooserTask();
            ph.Completed += ph_Completed;
            ph.Show();

        }

        /// <summary>
        /// Sharing the photo to social media including email
        /// </summary>
        void ph_Completed(object sender, PhotoResult e)
        {

            ShareMediaTask smt = new ShareMediaTask();
            smt.FilePath = e.OriginalFileName;
            smt.Show();
        }

Hope this helps!

Note: Please ignore or remove answer if you are developing universal wp application.

asitis
  • 3,023
  • 1
  • 31
  • 55
0

I believe in WP8.1 you would require to implement the IContinuationManager which will help you get the image selected by the user. First you need an event where you open up the Gallery

 private void PickAFileButton_Click(object sender, RoutedEventArgs e)
    {
        ...
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        // Launch file open picker and caller app is suspended
        // and may be terminated if required
        openPicker.PickSingleFileAndContinue();
    }

Once the image is selected then OnActivated() method in app.xaml.cs is called where you need continuation manager to get the data of file selected.

protected async override void OnActivated(IActivatedEventArgs args)
    {
        base.OnActivated(args);
        var continuationManager = new ContinuationManager();
        var rootFrame = CreateRootFrame();
        await RestoreStatusAsync(args.PreviousExecutionState);

        if (rootFrame.Content == null)
        {
            rootFrame.Navigate(typeof(MainPage));
        }

        var continuationEventArgs = e as IContinuationActivatedEventArgs;
        if (continuationEventArgs != null)
        {
            Frame scenarioFrame = MainPage.Current.FindName("ScenarioFrame") as Frame;
            if (scenarioFrame != null)
            {
                // Call ContinuationManager to handle continuation activation
                continuationManager.Continue(continuationEventArgs, scenarioFrame);
            }
        }

        Window.Current.Activate();
    }

Now the continuation manager will handle the activation for you.

        case ActivationKind.PickSaveFileContinuation:
            var fileSavePickerPage = rootFrame.Content as IFileSavePickerContinuable;
            if (fileSavePickerPage != null)
            {
                fileSavePickerPage.ContinueFileSavePicker(args as FileSavePickerContinuationEventArgs);
            }
            break;

        case ActivationKind.PickFolderContinuation:
            var folderPickerPage = rootFrame.Content as IFolderPickerContinuable;
            if (folderPickerPage != null)
            {
                folderPickerPage.ContinueFolderPicker(args as FolderPickerContinuationEventArgs);
            }
            break;

        case ActivationKind.WebAuthenticationBrokerContinuation:
            var wabPage = rootFrame.Content as IWebAuthenticationContinuable;
            if (wabPage != null)
            {
                wabPage.ContinueWebAuthentication(args as WebAuthenticationBrokerContinuationEventArgs);
            }
            break;
    }

All the code snippets are available here https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx

Once this is done you can fetch the data and save it to a file and attach that file to email.

EmailMessage email = new EmailMessage();
email.To.Add(new EmailRecipient("test@abc.com"));
email.Subject = "Test";
var file = await GetFile();
email.Attachments.Add(new EmailAttachment(file.Name, file));
await EmailManager.ShowComposeNewEmailAsync(email);

More details in this link http://developerpublish.com/windows-phone-8-1-and-windows-runtime-apps-how-to-2-send-emails-with-attachment-in-wp-8-1/

Hope this helps!

CodeNoob
  • 757
  • 5
  • 15