How do i share a png with RequestAsync ShareFileRequest?

Hello Xamarin Community Forum,

I can't find out how to share an image(png) with Xamarin.Essentials; how do you do it?

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Use Share.RequestAsync(ShareFileRequest) method to show the user interface to share a file.

    var file = Path.Combine(FileSystem.CacheDirectory, "img.png");
    
    await Share.RequestAsync(new ShareFileRequest
    {
        Title = "share_image",
        File = new ShareFile(file)
    });
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/essentials/share?context=xamarin/android&tabs=android#files

  • LoLeHLoLeH DKMember ✭✭

    @Jarvan allready tryed that, but i get this error and can't find any solutions.

  • JarvanJarvan Member, Xamarin Team Xamurai

    @LoLeH
    Have you installed the Xamarin.Essentials nuget package?

  • LoLeHLoLeH DKMember ✭✭

    @Jarvan yes i have. you need to add this "ExperimentalFeatures.Enable(ExperimentalFeatures.EmailAttachments, ExperimentalFeatures.ShareFileRequest);" and now its works but the image cant be copyed, dont know why.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to convert the image to bytes, then use File.WriteAllBytes method.

    var img = "xx.png";
    var file = Path.Combine(FileSystem.CacheDirectory, img);
    
    //convert the image to bytes
    
    File.WriteAllBytes(file, imgByteArray);
    
    await Share.RequestAsync(new ShareFileRequest
    {
        Title = Title,
        File = new ShareFile(file)
    });
    
Sign In or Register to comment.