Help creating a file

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

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

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

    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 Answers by the question author, which helps users to know the answer solved the author's problem.