1

I have a folder, called MyFolder, in the android device internal storage root directory. There is no external sd card mounted. The folder can be checked using says, ES file manager I want to write a file to that directory. I try the followings but seem all are not what I want. So how should sd be? Please help.

    File sd = Environment.getExternalStorageDirectory();
    //      File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)  ;
    //      File sd = new File( Environment.getExternalStorageDirectory().getAbsolutePath());
    //      File sd = Environment.getRootDirectory()  ; // system
    //      File sd = Environment.getDataDirectory()  ; 


    backupDBPath = "MyFolder/_subfolder/mydata.txt";

    File backupDB = new File(sd, backupDBPath);
4
  • is this an easy or difficult question? Jan 17, 2018 at 15:17
  • I check again the path of it on ES file manager and it is "mnt\sdcard\Myfolder" and the file is written there. I should have tested and debugged it more carefully. Jan 17, 2018 at 15:28
  • You asked how to store a file on internal storage, not external storage
    – matdev
    Jan 17, 2018 at 15:37
  • This is how I see it. I connect the android device via the Windows 10 desktop. Find the device on the window folder. Click on an icon labelled "internal storage" and it shows all the folders include \MyFolder. The path shown by ES file manager on the android device is , unexpectedly, "mnt\sdcard\Myfolder" Jan 18, 2018 at 0:02

2 Answers 2

7

If your directory located in the internal data directory of your app, you could write the code.

File directory = new File(this.getFilesDir()+File.separator+"MyFolder");

        if(!directory.exists())
            directory.mkdir();

        File newFile = new File(directory, "myText.txt");

        if(!newFile.exists()){
            try {
                newFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try  {
            FileOutputStream fOut = new FileOutputStream(newFile);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fOut);
            outputWriter.write("Test Document");
            outputWriter.close();

            //display file saved message
            Toast.makeText(getBaseContext(), "File saved successfully!",
                    Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            e.printStackTrace();
        }

From the official document: https://developer.android.com/guide/topics/data/data-storage.html#filesExternal

The device has removable(SD Card) or non-removable storage(Internal shared storage). Both are called external storage. Suppose you could create the directory in "Internal Shared storage", you could write the code below.

File directory = new File(Enviroment.getExternalStorage+File.separator+"MyFolder");//new File(this.getFilesDir()+File.separator+"MyFolder");

Note: If you have to use getExternalStorage, you should give the storage permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-1

From an app, you cannot write a file anywhere you'd like on the device's internal storage, it has to be located in the app's internal directory or app cache directory.

When saving a file to internal storage, you can acquire the appropriate directory as a File by calling one of two methods:

getFilesDir() Returns a File representing an internal directory for your app.

getCacheDir() Returns a File representing an internal directory for your app's temporary cache files.

You could write:

String backupDBPath = "/_subfolder/";
String fileName = "mydata.txt";
File file = new File(context.getFilesDir() + backupDBPath, filename);

More infos here:

https://developer.android.com/training/data-storage/files.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.