以下是我所做的事情:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
和
public void createMyFolder(){
File directory = new File(Environment.getExternalStorageDirectory().getPath() + "/myfolder/");
directory.mkdir(); //I had also tried mkdirs()
File file= new File(Environment.getExternalStorageDirectory().getPath() + "/t1.dat");
try {
file.createNewFile();
} catch (IOException e) {}
}
我测试了3个设备,其中一个设备抛出异常:
java.io.IOException: Cannot create dir /mnt/sdcard/myfolder
t1.dat在/ mnt / sdcard /中成功创建,但myfolder未成功创建。
该设备是Android版4.0.4的Xperia Ion。它有什么问题,我该如何解决?
编辑:我曾尝试过某些应用程序创建文件夹,例如文件管理器。 尽管SD卡是可写的和可读的,但它们也未能创建。 我认为我的手机有一些&#34;保护&#34;这不允许我在SD卡中创建文件夹。 但有趣的是,我的手机允许我创建文件。
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
答案 0 :(得分:0)
将Environment.getExternalStorageDirectory()替换为目录,“/ myfolder”将为“/ myfolder”
public void createMyFolder(){
File directory = new File(Environment.getExternalStorageDirectory().getPath() + "/myfolder");
directory.mkdir(); //I had also tried mkdirs()
File file= new File(directory.getPath() + "/t1.dat");
try {
file.createNewFile();
} catch (IOException e) {}
}