我无法在android外部存储目录中创建文件夹。
我在载货单上加上了遗漏,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
下面是我的代码:
String Path = Environment.getExternalStorageDirectory().getPath().toString()+ "/Shidhin/ShidhiImages";
System.out.println("Path : " +Path );
File FPath = new File(Path);
if (!FPath.exists()) {
if (!FPath.mkdir()) {
System.out.println("***Problem creating Image folder " +Path );
}
}
发布于 2014-07-16 20:54:00
如下所示:
String folder_main = "NewFolder";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
如果您想在其中创建另一个文件夹:
File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_main, "product1");
if (!f1.exists()) {
f1.mkdirs();
}
发布于 2014-07-16 20:51:06
mkdir
和mkdirs
之间的区别在于,mkdir
不会创建不存在的父目录,而mkdirs
会创建父目录,因此如果Shidhin
不存在,mkdir
将失败。此外,仅当创建了目录时,mkdir
和mkdirs
才返回true。如果该目录已经存在,则返回false
发布于 2020-02-27 00:39:17
现在不建议使用Environment.getExternalStorageDirectory(),因为API级别29,该选项使用:
Context.getExternalFilesDir()。
示例:
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(R.drawable.balloons);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
void deleteExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
file.delete();
}
boolean hasExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
return file.exists();
}
https://stackoverflow.com/questions/24781213
复制相似问题