【问题标题】:java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied)java.io.FileNotFoundException:/storage/sdcard/MyApp/questions.txt:打开失败:EACCES(权限被拒绝)
【发布时间】:2015-08-06 15:49:21
【问题描述】:

我在尝试保存文件时遇到问题。首先,当我在旧的 Eclipse 上尝试它时它可以正常工作,但是我必须使用新的 Eclipse,所以我从旧的 Eclipse 导出项目并在新的 Eclipse 上导入它。现在它不保存并给我这个例外:

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

我把我用来保存的代码(这个代码在旧的eclipse上工作,在新的给我例外):

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

还有我的清单文件:

<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>

以及我的模拟器详细信息的图像:

谁能帮助我?我想像我在旧版本中那样保存文件,我不知道为什么不起作用。

【问题讨论】:

  • 您的文件是否真的存在。如果没有,那么您首先需要在阅读之前创建文件。
  • 我认为我正在创建文件,请参阅 saveDates 方法,我创建文件夹,这就是您创建文件的意思吗?
  • 我的 mFolder.mkdir() 返回 false,为什么?

标签: java android eclipse save android-manifest


【解决方案1】:

首先,您需要清单中的此权限才能读取文件:

&lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;

其次,您的文件是否真的存在。如果没有,那么您首先需要在将数据写入文件之前创建文件。

如何创建文件:How to create a file in Android?

创建文件的示例代码:

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

【讨论】:

  • 我认为我正在创建文件,请参阅 saveDates 方法,我创建文件夹,这就是您创建文件的意思吗?
  • 我查看了我的代码,发现 mkdir 没有做任何事情,我做了: if(mFolder,mkdir()) ----- else ------- 它没有'不输入 if 或 else,它只是出去
  • 我的 mFolder.mkdir() 返回 false,为什么?
  • 是:05-25 05:29:23.830:I/System.out(15638):假 05-25 05:29:23.840:I/System.out(15638):假 05- 25 05:29:23.860:W/System.err(15638):java.io.FileNotFoundException:/storage/sdcard/MyApp/questions.txt:打开失败:EACCES(权限被拒绝)放置logcat的错误值是我的System.out.println(mFolder.mkdir());输出。使用 System.out.println(mFolder.mkdirs());它也返回 false
  • @Imrik 在其他设备上尝试,不知道这是否有帮助,但rebuild 你的项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2021-05-15
  • 2023-03-03
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 2015-06-27
相关资源
最近更新 更多