Note that there are some explanatory texts on larger screens.

plurals
  1. POOne little problem when exporting a file to SDCARD
    text
    copied!<p>I'm facing a problem: I have a stored data on /data/data/files and o have a menu button "export", when i tap on it the file is well exported in the SDCARD but with 0 as size (no data inside the file). export.class code:</p> <pre><code>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)) &gt; 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(); } } } </code></pre> <p>Code where to write to the file:</p> <pre><code>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 = ""; </code></pre> <p>WriteSettings method:</p> <pre><code>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(); </code></pre> <p>Menu export button:</p> <pre><code>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; </code></pre> <p>Edited menu export button:</p> <pre><code>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; </code></pre> <p>Thank you for your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload