我在外部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());
答案 0 :(得分:2)
尝试使用:
copyfile(nom, nmea_file.getAbsolutePath());
代替。
您从"/mnt/sdcard"
变量中获得一个root
,从sdCard
变量中获得另一个变种。
答案 1 :(得分:2)
因为你这样做了 你的nmea_file已经基于root了.. copyfile(nom, sdCard.getAbsolutePath()+ nmea_file )