我正在sdcard中导出文件,但是,我正面临一个FileNotFound异常(04-12 01:26:18.494: DEBUG/Carburant(4568): /mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat (Is a directory)
)这是代码:
try {
File sdCard = Environment.getExternalStorageDirectory();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.d("Carburant", "Sdcard can read/write !!");
mExternalStorageAvailable = mExternalStorageWriteable = true;
try {
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = context.getResources().getString(
R.string.fileName);
String fileDir = "" + preferences.getString("login", "")
+ "." + preferences.getString("marque", "") + ".";
File f2 = new File(context.getFilesDir(), fileDir
+ fileName);
String y = f2.getAbsolutePath();
Log.d("HI Export", y);
InputStream in = new FileInputStream(f2);
File dir = new File(sdCard.getAbsolutePath()
+ "/Carburant/");
String x = dir.getAbsolutePath();
Log.d("HI", x);
File file = new File(dir, fileDir + fileName);
file.mkdirs();
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
// out.flush();
in.close();
out.close();
Toast.makeText(context, "Export effectué",
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException ex) {
Toast.makeText(context, "File Not found",
Toast.LENGTH_SHORT).show();
String x = ex.getMessage();
Log.d("Carburant", x);
} catch (IOException e) {
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
}
}
// copyfile(nom,file.getAbsolutePath());
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.d("Carburant", "Sdcard only read !!");
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states,
// but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
} catch (Exception e) {
Log.d("CARBURANT", e.getMessage());
}
想要将文件从/data/data/<package name>/fileDir+fileName
导出到SD卡中的Carburant目录。
答案 0 :(得分:0)
File file = new File(dir, fileDir+fileName);
file.mkdirs();
我认为您错误地创建了一个名为/mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat
的目录,现在代码无法覆盖它?