简体   繁体   English

如何在下载目录中创建文件夹(Android Studio)

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

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. 我想在“下载”目录中创建一个目录(该目录存储我从网络下载的所有文件),以便将手机插入PC后可以在浏览器中看到它。

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

What should be the value of dir? dir的值应该是多少?

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 不要忘记询问棉花糖或更高版本的权限,并将写入存储权限添加到Android Manifest,例如: https : //stackoverflow.com/a/34722591/4479004

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();
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM