我正在尝试在外部目录上创建文件,如此
File sd = Environment.getExternalStorageDirectory();
if (sd == null) {
Toast.makeText(mContext,
"External Storage does not exist..No Export",
Toast.LENGTH_LONG).show();
return false;
}
File dataDir = new File(sd.getAbsolutePath()+ "/"+ DBHandler.folderName);
if(!dataDir.exists()){
dataDir.mkdir();
}
File wbfile = new File(dataDir.getAbsolutePath()+ "/"+filename+".xls");
但我在少数设备上获得以下内容
java.io.FileNotFoundException: /storage/emulated/0/AplicationXX Data/report.xls: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
FileOutputStream out = new FileOutputStream(wbFile);
任何想法可能是什么原因?
答案 0 :(得分:0)
您必须检查设备存储(内部或外部),如下所示
boolean mExternalStorageAvailable ;
boolean mExternalStorageWriteable ;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
//internalstorage stuff
}
并在android清单文件中添加权限,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />