Forum Xamarin.Forms

Xamarin Forms: Sharing Images Through Native picker

I am trying to share an image with an android picker through a dependency service but no images ever make it through to the application that is selected. The applications that are opened successfully just give a toast message stating "The attachment could not be loaded." Can someone please share why this piece of code could possibly be failing?

    public async Task ShareImageAndroid(string imageName)
    {
        var imagePath = "File:/" + DependencyService.Get<ImageCaching>().GetFilePath(imageName);
        Android.Net.Uri uri = Android.Net.Uri.Parse(imagePath);

        var sharingIntent = new Intent();
        sharingIntent.SetAction(Intent.ActionSend);
        sharingIntent.SetType("image/*");
        sharingIntent.PutExtra(Intent.ExtraText, "Image");
        sharingIntent.PutExtra(Intent.ExtraStream, uri);
        sharingIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
        Forms.Context.StartActivity(Intent.CreateChooser(sharingIntent, "Make a Selection"));
    }

For clarification:
DependencyService.Get().GetFilePath(imageName);
This function returns the path of an image that has already been saved to external storage on the device.

Here is an example of a path that is returned: "/data/user/0/com.application.name/files/assets/image.png"

Answers

  • Eric.ReevesEric.Reeves USMember ✭✭

    I think it may have something to do with the URI that is being returned. Does anyone know how the URI needs to be formatted and if I need to give android special permissions to access the documents folder? I have given my app access to external storage.

Sign In or Register to comment.