将现有图像附加到Xamarin中的Gmail

时间:2016-07-01 10:27:58

标签: android xamarin gmail xamarin.android

将现有图片附加到Gmail时,我遇到了一些问题, 这是我的代码

#region Attached Screenshot
        _imageView = FindViewById<ImageView>(Resource.Id.imageView);
        _imageView.Click += delegate
        {
            Intent = new Intent();
            Intent.SetType("image/*");
            Intent.SetAction(Intent.ActionGetContent);
            Intent.PutExtra("return-data", true);
            StartActivityForResult(
                Intent.CreateChooser(Intent, "Select Picture"), PICK_FROM_GALLERY);
        };
#endregion

从这段代码中我从用户那里获取了图像,但我不知道如何将该图像附加到此代码中,

#region SendMail

        Button sendButton = FindViewById<Button>(Resource.Id.button);

        sendButton.Click += delegate
        {
            var email = new Intent(Android.Content.Intent.ActionSend);
            email.PutExtra(Android.Content.Intent.ExtraEmail,
            new string[] { "person1@xyz.com", "person2@xyz.com" });
            email.PutExtra(Android.Content.Intent.ExtraCc,
            new string[] { "person3@xyz.com" });
            email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Email");
            email.PutExtra(Android.Content.Intent.ExtraText, "Hello from Mobility Team");

            email.SetType("message/rfc822");
            StartActivity(email);
        };

#endregion

我很长时间不推荐这个代码, 谢谢

1 个答案:

答案 0 :(得分:1)

使用Result.Data提供用户为Uri内容选择的项目的Intent.ExtraStream

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Main);
    Button button = FindViewById<Button>(Resource.Id.myButton);
    button.Click += delegate {
        Intent = new Intent();
        Intent.SetType("image/*");
        Intent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), 99);
    };
}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == 99)
    {
        var mailIntent = new Intent(Intent.ActionSend);
        mailIntent.SetType("message/rfc822");
        mailIntent.PutExtra(Intent.ExtraEmail, new string[] { "foobar@example.com" });
        mailIntent.PutExtra(Intent.ExtraSubject, "Email Subject");
        mailIntent.PutExtra(Intent.ExtraText, "Email Body");
        mailIntent.PutExtra(Intent.ExtraStream, data.Data);
        StartActivity(mailIntent);
    }
}