Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android将文件保存到SD卡,而不是内部存储_Android_File_External_Private - Fatal编程技术网

Android将文件保存到SD卡,而不是内部存储

Android将文件保存到SD卡,而不是内部存储,android,file,external,private,Android,File,External,Private,我知道这方面有很多问题,但出于某种原因,我似乎什么都做不到。 我正在尝试从我的应用程序中获取2个文本文件以保存到SD卡。它正确地创建了目录和文件,但始终是内部存储,而不是外部存储。我在清单中也有相应的权限 try { File sdCard = Environment.getExternalStorageDirectory(); File myFile = new File(sdCard.getAbsolutePath() + "/rlgl");

我知道这方面有很多问题,但出于某种原因,我似乎什么都做不到。 我正在尝试从我的应用程序中获取2个文本文件以保存到SD卡。它正确地创建了目录和文件,但始终是内部存储,而不是外部存储。我在清单中也有相应的权限

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();
        }
试试看{
文件sdCard=Environment.getExternalStorageDirectory();
File myFile=新文件(sdCard.getAbsolutePath()+“/rlgl”);
myFile.mkdir();
//myFile.createNewFile();
字符串newLine=System.getProperty(“line.separator”);
File File=新文件(myFile,“rlgls.txt”);
if(file.exists()){
}否则,如果(!file.exists()){
FileOutputStream fOut=新的FileOutputStream(文件);
OutputStreamWriter myOutWriter=
新的OutputStreamWriter(fOut);
对于(int i=0;i<30;i++){
myOutWriter.append(“0.0”+换行符);
}
myOutWriter.close();
fOut.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
这是我正在使用的代码。我已经按照其他Stackoverflow响应的指示进行了操作,但它从未进入SD卡。我做错什么了吗?还有一个后续问题,我是否有办法使用上述代码,以使文件对用户不可见。他们应该没有理由打开它们。提前谢谢

它正确地创建了目录和文件,但始终是内部存储,而不是外部存储

不,它将它们放在外部存储器上。用户所看到的内部存储就是开发人员所看到的。可通过
getFilesDir()
等方法访问。这些都不是,比如某种形式的SD卡

还有一个后续问题,我是否有办法使用上述代码,以使文件对用户不可见。他们应该没有理由打开它们

然后把它们放在内部存储器中

如果文件名前有“.”,则我的应用程序无法读取/写入这些文件


我觉得很难相信。
前缀使它们默认情况下不会显示在某些文件浏览器中,但仅此而已。用户可以访问它们(如果它们在外部存储上),应用程序也可以访问它们(遵循与任何其他文件相同的规则,即没有前导的
)。

Android将保存到默认存储上。您必须在设备系统设置中手动设置默认存储。Android API实际上并没有为您提供获取设备SD卡的方法。此方法将返回默认的共享存储,它可能是SD卡,也可能不是SD卡。谢谢您提供的信息。我还有很多东西要学。不过我还有一个问题。我知道在文件或目录名前加“.”会使其不可见,但这是否也意味着应用程序也无法读取它?我正在进行试验,当文件名前面有一个“.”时,我的应用程序无法读取/写入文件。再次测试后,我意识到如果我在文件名前面放一个“.”,它仍然可以写入/读取。但是,如果我在目录名前面加一个“.”来隐藏文件夹,它就不能。不幸。