Help with sharing files

Eduardo Gomez 3,426 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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,936 Reputation points Microsoft Vendor
    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful