【发布时间】: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