简体   繁体   中英

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

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:

在此处输入图片说明

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM