Share via

Help with sharing files

Eduardo Gomez 4,316 Reputation points
2022-04-01T10:10:41.633+00:00

I am trying to share files, using Android specific code, but the DocumentUri is null

Android depency

public class Share : IShare {
public Task Show(string title, string messge, string filePath, string ext) {

        var intent = new Intent(Intent.ActionSend);
        if (ext.Equals("pdf")) {
            intent.SetType("application/pdf");
        } else if (ext.Equals("docx")) {
            intent.SetType("application/msword");
        } else {
            intent.SetType("text/plain");
        }
        var documentUri = FileProvider.GetUriForFile(Android.App.Application.Context,
            "com.companyname.XamNotes.provider",
            new File(filePath));

        intent.PutExtra(Intent.ExtraText, title);
        intent.PutExtra(Intent.ExtraSubject, messge);
        intent.PutExtra(Intent.ExtraStream, documentUri);

        var chooserIntent = Intent.CreateChooser(intent, title);
        chooserIntent.SetFlags(ActivityFlags.GrantReadUriPermission);
        chooserIntent.SetFlags(ActivityFlags.NewTask);

        Android.App.Application.Context.StartActivity(chooserIntent);

        return Task.FromResult(true);
    }
}

https://github.com/eduardoagr/DuoNotes

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

Anonymous
2022-04-04T01:43:56.85+00:00

Hello,

Your saved file path comes from: FileSystem.CacheDirectory;

You need to change <files-path> to <cache-path> in file_provider_paths.xml file like following code.

   <?xml version="1.0" encoding="UTF-8" ?>  
   <paths xmlns:android="http://schemas.android.com/apk/res/android">  
   	<cache-path name="external_files" path="."/>  
   </paths>  

If you want to know more details about Path maps, you can refer to this thread: https://developer.android.com/reference/androidx/core/content/FileProvider

Best Regards,
Leon Lu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.