开发者

No such file or directory in Android

I'm facing a problem when copying a file in the external sdcard: here is what i get in the log message:

04-06 17:52:36.804: DEBUG/Carburant(258): Sdcard can read/write !!
04-06 17:52:36.864: DEBUG/Carburant(258): /mnt/sdcard/mnt/sdcard/settings.dat (No such file or directory)

And here is my code:

public class Import {
    private Context context;
    private String nom;


    public Import(Context context,String nom) {
        this.context = context;
        this.nom=nom;
       }

    public void transfer(){

    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;
        File root = Environment.getExternalStorageDirectory();
        File nmea_file = new File(r开发者_如何转开发oot,"settings.dat");
        copyfile(nom,sdCard.getAbsolutePath() + nmea_file);
    } 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;
    }

And here is my copyfile func:

private void copyfile(String srFile, String dtFile){
        try{
            File f1 = new File(srFile);
            File f2 = new File(dtFile);
          InputStream in = new FileInputStream(f1);
          OutputStream out = new FileOutputStream(f2);

          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0){
            out.write(buf, 0, len);
          }
          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();      
        }
      }

i don't know why the compiler gives /mnt/sdcard/mnt/sdcard/settings.dat as output file, there is duplicate in the /mnt/sdcard... Thank you for your help.

File dir = new File (sdCard.getAbsolutePath() + "/Carburant/");
        dir.mkdirs();
        File file = new File(dir, "settings.dat");
        copyfile(nom,dir.getAbsolutePath());


Try using:

copyfile(nom, nmea_file.getAbsolutePath());

instead.

You are getting one "/mnt/sdcard" from the root variable and another one from the sdCard variable.


because you do this your nmea_file has already based on the root.. copyfile(nom,sdCard.getAbsolutePath()+ nmea_file)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜