简体   繁体   中英

Android saving file to SD Card, not internal storage

I know there have been questions about this, but for some reason nothing seems to work for me. I'm trying to get 2 text files to save to the SD card from my app. It correctly creates the directory and the files, but always to the Internal Storage, never the External Storage. I do have the permissions in place as well in the Manifest.

try {
        File sdCard = Environment.getExternalStorageDirectory();
        File myFile = new File(sdCard.getAbsolutePath() + "/rlgl");
        myFile.mkdir();
       // myFile.createNewFile();
        String newLine = System.getProperty("line.separator");
        File file = new File(myFile, "rlgls.txt");
        if(file.exists())  {

        } else if (!file.exists()){
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        for (int i = 0; i < 30; i++) {
        myOutWriter.append("0.0" + newLine);
        }
        myOutWriter.close();
        fOut.close();
        }
        } catch (IOException e) {
            e.printStackTrace();
        }

This is the code that I am using. I've followed directions from other Stackoverflow responses but it never goes to the SD Card. Is there something I'm doing wrong? Also a follow up question is there a way for me to use the above code in order to make the files invisible to the user. They should have no reason to open them. Thanks in advance.

It correctly creates the directory and the files, but always to the Internal Storage, never the External Storage

No, it places them on external storage. What the user sees as internal storage is what the developer sees as external storage . Internal storage is accessed via methods like getFilesDir() . And none of those are removable storage , such as some form of SD card.

Also a follow up question is there a way for me to use the above code in order to make the files invisible to the user. They should have no reason to open them.

Then put them on internal storage.

my app can't read/write from/to the files when there is a "." in front of their names

I find that very difficult to believe. The . prefix makes them not show up by default in some file browsers, but that's it. Users can get to them (if they are on external storage), and apps can get to them (subject to the same rules as any other files, those without a leading . ).

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