【问题标题】:Sharing image via my app in xamarin.forms通过我在 xamarin.forms 中的应用程序共享图像
【发布时间】:2016-12-28 09:05:30
【问题描述】:

我想分享一张图片。共享选项应该包含我的应用 xyz。例如,如果我打开一张图片并想在 instagram 上分享它,分享选项包含 instagram、facebook、twitter、电子邮件等。就像我的应用程序应该在分享选项中一样。如何在 xamarin.forms(ios 和 android)中做到这一点。

【问题讨论】:

    标签: xamarin.ios xamarin.android xamarin.forms


    【解决方案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 权限。

    让我知道这是否有效。

    【讨论】:

    • 我想做完全相反的事情。 @sumeet
    • 我希望我的应用在共享选项中。
    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    相关资源
    最近更新 更多