Возникает проблема при копировании файла на внешнюю SD-карту:
вот что я получаю в сообщении журнала:
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)
А вот и мой код:
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(root,"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;
}
А вот моя функция copyfile:
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();
}
}
Я не знаю, почему компилятор выдает /mnt/sdcard/mnt/sdcard/settings.dat в качестве выходного файла, в / mnt / sdcard есть дубликат ...
Спасибо за вашу помощь.
File dir = new File (sdCard.getAbsolutePath() + "/Carburant/");
dir.mkdirs();
File file = new File(dir, "settings.dat");
copyfile(nom,dir.getAbsolutePath());