java.io.FileNotFoundException:/storage/sdcard/MyApp/questions.txt:打开失败:EACCES(权限被拒绝)
java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied)
我在尝试保存文件时遇到问题。首先,当我在较旧的 Eclipse 上尝试时它工作正常,但我必须使用新的 Eclipse,所以我从较旧的 Eclipse 导出项目并将其导入新的 Eclipse。现在它不保存并给我这个异常:
java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied)
我把我用来保存的代码(这段代码适用于旧的 Eclipse,在新的 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>
以及我的模拟器详细信息的图片:
谁能帮帮我?我想像老版本一样保存文件,不知道为什么不行。
首先,您需要清单中的此权限才能读取文件:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
其次,你的文件是否真实存在。如果没有,那么您首先需要在向其写入数据之前创建该文件。
如何创建文件: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);
}
我在尝试保存文件时遇到问题。首先,当我在较旧的 Eclipse 上尝试时它工作正常,但我必须使用新的 Eclipse,所以我从较旧的 Eclipse 导出项目并将其导入新的 Eclipse。现在它不保存并给我这个异常:
java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied)
我把我用来保存的代码(这段代码适用于旧的 Eclipse,在新的 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>
以及我的模拟器详细信息的图片:
谁能帮帮我?我想像老版本一样保存文件,不知道为什么不行。
首先,您需要清单中的此权限才能读取文件:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
其次,你的文件是否真实存在。如果没有,那么您首先需要在向其写入数据之前创建该文件。
如何创建文件: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);
}