How to create a folder in Download directory (Android Studio)

时光怂恿深爱的人放手 提交于 2021-01-28 10:37:23

问题


I'd like to create a directory in a Download directory (the one which stores all the files I download from web) so I can see it in browser after I plug the mobile into my PC.

File myDirectory = new File(dir, "NewDirectory");

What should be the value of dir?


回答1:


File dir = new File(Environment.getExternalStorageDirectory() + "/Download/your folder/");
dir.mkdirs(); // creates needed dirs

Don't forget to ask for permissions on Marshmallow or newer and to add the write storage permission to Android Manifest, for example: https://stackoverflow.com/a/34722591/4479004




回答2:


try following code

private void createDirectoryAndSaveFile() {

File direct = new File(Environment.getExternalStorageDirectory() + "/Download/DirName");

if (!direct.exists()) {
    File wallpaperDirectory = new File("/sdcard/Download/DirName/");
    wallpaperDirectory.mkdirs();
 }
}


来源:https://stackoverflow.com/questions/36695734/how-to-create-a-folder-in-download-directory-android-studio

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