Try to search your question here, if you can't find : Ask Any Question Now ?

Android App giving error #CS1503 Argument 1: cannot convert from 'Java.IO.FileOutputStream' to 'System.IO.Stream'

HomeCategory: stackoverflowAndroid App giving error #CS1503 Argument 1: cannot convert from 'Java.IO.FileOutputStream' to 'System.IO.Stream'
Avatarwillson asked 5 days ago

I am attempting to write some text to myFile.csv in Android App but I am getting following errors and am unable to resolve it.

Error CS0103 The name 'MODE_APPEND' does not exist in the current context

Error CS1503 Argument 1: cannot convert from 'Java.IO.FileOutputStream' to 'System.IO.Stream

I made use of code chunk given at this link [1]:saving a text or CSV file with strings from edittext

using Android.App;
using Android.Content;
using Android.OS;
using Android.Speech;
using Android.Support.V7.App;
using Android.Widget;
using System;
using System.Text;
using Java;
using Java.IO;
using Xamarin.Forms;

   protected override void OnActivityResult(int requestCode, Result resultVal, Intent data)
        {
            if (requestCode == VOICE)
            {
                if (resultVal == Result.Ok)
                {
                    var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
                    if (matches.Count != 0)
                    {
                        ///Exporting to CSV
                        try
                        {
                            File yourFile = new File("myFile.csv");
                            yourFile.CreateNewFile(); // if file already exists will do nothing 
                            FileOutputStream oFile = new FileOutputStream(yourFile, false);
                            //FileOutputStream oFile = new FileOutputStream(yourFile, false);

//Here the First Error appears
                            FileOutputStream oFile = OpenFileOutput("DailyExpense.csv", MODE_APPEND); 

//Here the Second Error Appears
                            OutputStreamWriter outputWriter = new OutputStreamWriter(oFile);
                            outputWriter.Write(GetOrderText("string from edittext", "string from edittext", "string from edittext"));
                            outputWriter.Write("n");
                            outputWriter.Close(); 
                        }
                        catch (Exception e)
                        {
                            // e.printStackTrace();
                            MessagingCenter.Send(this, "No Data To Export " + e.Message);
                        }
                        ///Exporting to CSV Ends
                    }
                    else
                        textBox.Text = "No speech was recognised";
                    // change the text back on the button
                    recButton.Text = "Start Recording";
                }
            }
            base.OnActivityResult(requestCode, resultVal, data);
        }
        private String GetOrderText(String aa, String bb, String cc)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(aa + ";");
            sb.Append(" " + bb + ";");
            sb.Append(" " + cc);
            return sb.ToString();
        }

After this, I tried another approach using Xaramin. The Code is as follows:-

private async Task SaveCountAsync(string outLn)
        {
            try
            {
              //  var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
                var path = Xamarin.Essentials.FileSystem.AppDataDirectory;
                string filename = Path.Combine(path, "myFile.csv");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                using (StreamWriter objStreamWriter = new StreamWriter(filename, true))
                {
                    //  objStreamWriter.WriteLine(outLn);
                    await objStreamWriter.WriteLineAsync(outLn).ConfigureAwait(false);
                    objStreamWriter.Close();

                }
            }
            catch (Exception e)
            {
                // e.printStackTrace();
                MessagingCenter.Send(this, "No Data To Export " + e.Message);
            }
}

There are no errors in the Xaramin Code but there is no output file created at the desired path that is

/data/user/0/com.raks.myFolder/files/myFile.csv

1 Answers
Best Answer
AvatarAmit answered 5 days ago
Your Answer

1 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com