简体   繁体   中英

Save String to a specific folder on internal storage - Android

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();
                }

            }
        });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM