首页 文章

通过我的应用程序在xamarin.forms中共享图像

提问于
浏览
0

我想分享一个图像 . 共享选项应包含我的应用xyz . 例如,如果我打开一个图像,并希望在Instagram上分享它共享选项包含instagram,脸书,推特,电子邮件等 . 就像我的应用程序应该在共享选项 . 我怎么能在xamarin.forms(ios和android)中做到这一点 .

1 回答

  • 1

    我认为应用程序图标是在您的应用程序专用的目录中创建的,因此其他应用程序无法获取它 .

    您需要将其保存在其他应用程序可以访问的位置,然后从该位置共享它,如下所示:

    public void Share (string title, string content)
    {
        if (string.IsNullOrEmpty (title) || string.IsNullOrEmpty (content))
                    return;
    
        Bitmap b = BitmapFactory.DecodeResource(Resources,Resource.Drawable.icon_120);
    
        var tempFilename = "test.png";
        var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        var filePath = System.IO.Path.Combine(sdCardPath, tempFilename);
        using (var os = new FileStream(filePath, FileMode.Create))
        {
            b.Compress(Bitmap.CompressFormat.Png, 100, os);
        }
        b.Dispose ();
    
        var imageUri = Android.Net.Uri.Parse ($"file://{sdCardPath}/{tempFilename}");
        var sharingIntent = new Intent ();
        sharingIntent.SetAction (Intent.ActionSend);
        sharingIntent.SetType ("image/*");
        sharingIntent.PutExtra (Intent.ExtraText, content);
        sharingIntent.PutExtra (Intent.ExtraStream, imageUri);
        sharingIntent.AddFlags (ActivityFlags.GrantReadUriPermission);
        StartActivity (Intent.CreateChooser (sharingIntent, title));
    }
    

    还要为您的应用添加ReadExternalStorage和WriteExternalStorage权限 .

    如果有效,请告诉我 .

相关问题