在应用程序目录中创建文件夹
Creating folder in the application directory
我已经使用 getFilesDir() 在应用程序目录中创建了一个文件夹,它给出了应用程序目录的路径如下
/data/data/{my application package}/files
但是当我用它创建一个新文件夹时
File folder = new File(getFilesDir()
+ "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
我没有看到任何文件夹。此外,当我在 ES Explorer 中访问时,应用程序目录的实际路径是
/Android/data/{my package name}/files
我的问题是如何在应用程序目录中创建一个文件夹,以便在应用程序卸载时自动删除它。
改用方法Context.getDir()
。您不需要调用 mkdirs()
,因为 getDir()
会自动执行。
引自文档:
Retrieve, creating if needed, a new directory in which the application
can place its own custom data files. You can use the returned File
object to create and access files in this directory. Note that files
created through a File object will only be accessible by your own
application; you can only set the mode of the entire directory, not of
individual files.
通过使用 getDir()
来使用它
File dir = ctx.getDir("my_sub_dir", Context.MODE_PRIVATE);
File newfile = new File(topDirFile.getAbsolutePath() + File.separator + "new_file_name");
newfile.createNewFile();
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newfile));
我已经使用 getFilesDir() 在应用程序目录中创建了一个文件夹,它给出了应用程序目录的路径如下
/data/data/{my application package}/files
但是当我用它创建一个新文件夹时
File folder = new File(getFilesDir()
+ "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
我没有看到任何文件夹。此外,当我在 ES Explorer 中访问时,应用程序目录的实际路径是
/Android/data/{my package name}/files
我的问题是如何在应用程序目录中创建一个文件夹,以便在应用程序卸载时自动删除它。
改用方法Context.getDir()
。您不需要调用 mkdirs()
,因为 getDir()
会自动执行。
引自文档:
Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.
通过使用 getDir()
File dir = ctx.getDir("my_sub_dir", Context.MODE_PRIVATE);
File newfile = new File(topDirFile.getAbsolutePath() + File.separator + "new_file_name");
newfile.createNewFile();
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newfile));