Share via

Help creating a file

Eduardo Gomez 4,316 Reputation points
2022-02-23T17:04:23.267+00:00

Hello
I am trying to create txt

  var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            var file = Path.Combine(localFolder, "Reports.txt");

            using (StreamWriter writer = new StreamWriter(Path.GetFullPath(file))) {

                foreach (var ce in CategoryExpenses) {

                    writer.WriteLine($"{ce.Category} - {ce.Porcentage}");
                }
            }
            var share = DependencyService.Get<IShare>();

            share.Show("Expense report", "Here is your report", file);

Java.Lang.IllegalArgumentException: 'Failed to find configured root that contains /data/data/com.companyname.budget/files/.local/share/Reports.txt'

https://github.com/eduardoagr/Xamarin-Master-Class

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

Anonymous
2022-02-24T05:41:29.087+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

Please change your var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); to var localFolder= FileSystem.AppDataDirectory;, **

FileSystem.AppDataDirectory** comes from Xamarin.Essentialas.

Then open your fille_provider_paths.xml, change files-path like following code.

   <?xml version="1.0" encoding="utf-8" ?>  
   <paths>  
   <files-path name="files" path="."/>  
   </paths>  

Note: Provider path for a specific path as followings:

   <files-path/> --> Context.getFilesDir()  
   <cache-path/> --> Context.getCacheDir()  

If you have some doubts about using FileProvider, you can refer to google's document.

And if you want to share this text file to other application, you need to add chooserIntent.SetFlags(ActivityFlags.NewTask); for chooserIntent in the Share.cs. Otherwise, you will get following exception.

   Android.Util.AndroidRuntimeException: 'Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?'  

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.