Creating folder in the application directory

只谈情不闲聊 提交于 2019-12-06 04:17:42

问题


i have used getFilesDir() to create a folder in the application directory, it gives the path of the applicatoin directory as follows

/data/data/{my application package}/files

but when i use it to create a new folder using

File folder = new File(getFilesDir()
            + "/MyFolder");
    if (!folder.exists()) {
        folder.mkdir();
    }

i don't see any folder. Also when i access in ES Explorer the actual path of the application directory is

/Android/data/{my package name}/files

My question is how to create a folder in the application directory so that it can be deleted automatically on application uninstallation.


回答1:


Use method Context.getDir() instead. You don't need to invoke mkdirs(), because getDir() will do it automatically.

Quote from the documentation:

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.




回答2:


Use this by making a use of 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));


来源:https://stackoverflow.com/questions/28958029/creating-folder-in-the-application-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!