У меня проблема: у меня есть сохраненные данные в / data / data / files и у меня есть кнопка меню "export", когда я нажимаю на нее, файл хорошо экспортируется в SDCARD, но с размером 0 (нет данных внутри файла).
код экспорта.класса:
public Export(Context context,String nom) {
this.context = context;
this.nom=nom;
}
public void transfer(){
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;
File dir = new File (sdCard.getAbsolutePath() + "/Carburant/");
dir.mkdirs();
File file = new File(dir, "settings.dat");
//FileOutputStream f = new FileOutputStream(file);
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());
}
}
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();
}
}
}
Код для записи в файл:
if (data != "" ) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context,fileDir+fileName, data);
data = "";
Метод WriteSettings:
public static void WriteSettings(Context context, String nom, String data) {
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
fOut = context.openFileOutput(nom, Context.MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
osw.close();
fOut.close();
Кнопка экспорта меню:
case R.id.export:
String mainDirPath = this.getFilesDir() + File.separator + "settings.dat";
FileOutputStream fos;
try {
fos = this.openFileOutput("settings.dat", Context.MODE_PRIVATE);
Export myExport = new Export(this,mainDirPath);
myExport.transfer();
} catch (FileNotFoundException e) {
Log.d("Carburant","File Not found");
}catch(IOException e){
Log.d("Carburant","ERROR");
}
return true;
Отредактированная кнопка экспорта меню:
case R.id.exporter:
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
String mainDirPath = this.getFilesDir() + File.separator + fileDir+fileName;
Log.d("Carburant",mainDirPath);
FileOutputStream fos;
try {
fos = this.openFileOutput(fileDir+fileName, Context.MODE_PRIVATE);
Export myExport = new Export(this,mainDirPath);
myExport.transfer();
} catch (FileNotFoundException e) {
Log.d("Carburant","File Not found");
}catch(IOException e){
Log.d("Carburant","ERROR");
}
return true;
Спасибо за вашу помощь.