1

I am basically trying to save a String to a specific folder I create on the internal storage of my phone when I click the button save. I am still a noob at these stuff therefore I do not know what to do. I want to access the file using the file manager and not through the application I created. So I need help with: 1- Create the folder. 2- Save the string to that specific folder. Any help is appreciated and thank you in advance.

Save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(), ("X values: "+mg1.getXs()) ,Toast.LENGTH_LONG).show();
                //Toast.makeText(getApplicationContext(), SENSOR_READING_STRING ,Toast.LENGTH_LONG).show(); 

                String filename = "Data.txt";
                String ABCD_STRING = "SENSOR_READING_STRING";
                FileOutputStream outputStream;

                try {
                  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
                 outputStream.write(ABCD_STRING.getBytes());
                  outputStream.close();
                } catch (Exception e) {
                  e.printStackTrace();
                }

            }
        });

1 Answers1

0

By default, when saving to internal storage. The data is private and would only be accessible to the application that saved it. So if you want to be able to open the file through file manager you would need to change this line.

outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

Context.MODE_PRIVATE should be either MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE depending on your permission level needed.

More info on openFileOutput flags

shyamal
  • 826
  • 10
  • 16