2011-04-11 80 views
2

我导出文件的SD卡,但是,我面对一个FileNotFound异常(04-12 01:26:18.494: DEBUG/Carburant(4568): /mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat (Is a directory) )这里是代码:FileNotFoundException异常android系统中的SD卡

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

您的路径连接看起来很可疑。我建议插入一些魔术字符串,看看是否能解决问题。在加入文件名和目录时,我建议分别从第二个和第一个段的开始和结尾修剪所有的正斜杠字符,并在它们之间重新插入斜线。显然这不会处理“..”,“〜”或“。”,这也是你可能想要考虑的。 – 2011-04-11 23:38:02

+0

@计算机语言学家:不,“。”没有问题。因为我在其他类/活动中使用此文件并且没有遇到任何问题。创作道路上存在着绝对的错误。 :\ – androniennn 2011-04-11 23:45:33

+0

我不认为这个消息可能更清楚:'/mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat(是一个目录)'。更改'File file = new File(dir,fileDir + fileName); file.mkdirs();'到'File file = new File(dir,fileDir); file.mkdirs();文件=新文件(文件,文件名);'。这就是说你的代码是一团糟 - 终于使用,并且不要使用1000个变量 – 2013-09-16 10:27:48

回答

0
File file = new File(dir, fileDir+fileName); 
file.mkdirs(); 

我认为你已经创建了一个名为/mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat的目录,并且现在代码不能写过吗?

+0

我在这一行删除了fileName + fileDir:File dir = new File(sdCard .getAbsolutePath()+“/ Carburant /”); (见第1篇文章),总是有同样的问题:(。 – androniennn 2011-04-11 23:39:57

+0

@@ Preet Sangha:maked this:File file = new File(sdCard.getAbsolutePath(),fileDir + fileName); file.mkdirs();同样的问题! – androniennn 2011-04-11 23:49:24