write a file to a folder in the android device int

2020-08-09 06:41发布

问题:

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

回答1:

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" />


回答2:

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