2

I have a problem when I try to save a file. First of all, it worked correctly when I try it on an older Eclipse, but I have to use new Eclipse so I export the project from the older Eclipse and import it on the new. Now it doesn't save and give me this excepction:

java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied) 

I put the code I use to save (this code works on the older eclipse, in the new gives me the exception):

public void saveDates(boolean mainScreen) { String extr = Environment.getExternalStorageDirectory().toString(); File mFolder = new File(extr + "/MyApp"); if (!mFolder.exists()) { mFolder.mkdir(); } String strF = mFolder.getAbsolutePath(); File mSubFolder = new File(strF /*+ "/MyApp-SubFolder"*/); if (!mSubFolder.exists()) { mSubFolder.mkdir(); } //Nombre del fichero String s = "questions.txt"; BufferedWriter out; try { FileWriter fileWriter = new FileWriter(mSubFolder.getAbsolutePath() + "/" + s, false); out = new BufferedWriter(fileWriter); out.write(indexAnswer + ";"); out.write("\r\n"); out.write(finished + ";"); out.write("\r\n"); out.write(directricesGenerales + ";"); out.write("\r\n"); out.write(politicaAmbiental + ";"); out.write("\r\n"); out.write(planificacion + ";"); out.write("\r\n"); out.write(implementacion + ";"); out.write("\r\n"); out.write(verificacion + ";"); out.write("\r\n"); out.write(revision + ";"); out.write("\r\n"); out.write(definicionProyecto + ";"); out.write("\r\n"); out.write(analisisAmbiental + ";"); out.write("\r\n"); out.write(desarrollo + ";"); out.write("\r\n"); out.write(disenyoDetalle + ";"); out.write("\r\n"); out.write(planAccion + ";"); out.write("\r\n"); out.write(evaluacionContinua + ";"); out.write("\r\n"); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "Datos guardados.", Toast.LENGTH_LONG).show(); if(mainScreen) { // Cerrar la ventana de test y volver a la de inicio Intent intent = new Intent(); intent.setClass(questions.this, MainActivity.class); startActivity(intent); finish(); } } 

And my manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.XXX" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.ecotoolin.principal" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.ecotoolin.questions" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <activity android:name="com.example.ecotoolin.Chart" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <activity android:name="com.example.ecotoolin.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <activity android:name="com.example.ecotoolin.reload" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <activity android:name="com.example.ecotoolin.sendMail" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> </application> <uses-permission android:name="android.permission.INTERNET"> </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> 

And an image of my emulator details:

enter image description here

Can anyone help me? Iwant to save the file like I do it in the older version, I don't know why doesn't work.

3
  • does your file actually exist. If not then you first need to create the file before reading it. Commented May 25, 2015 at 8:51
  • i think i'm creating the file, see the saveDates method, I create the folder, is that what you mean with create the file? Commented May 25, 2015 at 8:52
  • my mFolder.mkdir() return false, why? Commented May 25, 2015 at 9:26

1 Answer 1

3

First, you need this permission in manifest to read files:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Second, does your file actually exist. If not then you first need to create the file before writing data to it.

How to create a file: How to create a file in Android?

Sample code to create a file:

// use "File.separator" instead of "/" File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt"); //create the file file.createNewFile(); //text you want to write to your file String text = "your_text"; //check if file exists if(file.exists()){ OutputStream fo = new FileOutputStream(file); //write the data fo.write(text); //close to avoid memory leaks fo.close(); //give a log message that the file was created with "text" System.out.println("file created: "+file); } 
Sign up to request clarification or add additional context in comments.

8 Comments

i think i'm creating the file, see the saveDates method, I create the folder, is that what you mean with create the file?
I review my code and I see that mkdir doesn't do anything, I do: if(mFolder,mkdir()) ----- else ------- and it doesn't enter on the if or on the else, it just go out of that
my mFolder.mkdir() return false, why?
yes: 05-25 05:29:23.830: I/System.out(15638): false 05-25 05:29:23.840: I/System.out(15638): false 05-25 05:29:23.860: W/System.err(15638): java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied) the false value that put the logcat was my System.out.println(mFolder.mkdir()); output. With System.out.println(mFolder.mkdirs()); it returns false too
@Imrik try on other devices, and don't know if this is gonna help but rebuild your project
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.