java.io.FileNotFoundException:/storage/sdcard/MyApp/questions.txt:打开失败:EACCES(权限被拒绝)
我试图保存文件时遇到问题。首先,当我在旧的Eclipse上尝试它时,它工作正常,但是我必须使用新的Eclipse,所以我从旧的Eclipse导出项目并在新的Eclipse上导入它。现在它没有保存并给我这个例外:java.io.FileNotFoundException:/storage/sdcard/MyApp/questions.txt:打开失败:EACCES(权限被拒绝),java,android,eclipse,save,android-manifest,Java,Android,Eclipse,Save,Android Manifest,我试图保存文件时遇到问题。首先,当我在旧的Eclipse上尝试它时,它工作正常,但是我必须使用新的Eclipse,所以我从旧的Eclipse导出项目并在新的Eclipse上导入它。现在它没有保存并给我这个例外: java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied) 我放置了我用来保存的代码(此代码在旧的eclipse上工作,在新的ec
java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied)
我放置了我用来保存的代码(此代码在旧的eclipse上工作,在新的eclipse中,这给了我一个例外):
和我的清单文件:
<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>
以及我的模拟器详细信息的图像:
有人能帮我吗?我想像在旧版本中一样保存文件,我不知道为什么不起作用。首先,您需要在清单中有此权限才能读取文件:
第二,您的文件是否确实存在。如果没有,那么您首先需要在将数据写入文件之前创建该文件 如何创建文件: 创建文件的示例代码:
// 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方法,我创建文件夹,这就是创建文件的意思吗?my mFolder.mkdir()返回false,为什么?我想我正在创建文件,请参阅saveDates方法,我创建文件夹,这就是你创建文件的意思吗?我检查了我的代码,我发现mkdir没有做任何事情,我做了:if(mFolder,mkdir())----else-----它没有进入if或else,它只是退出了,我的mFolder.mkdir()返回false,为什么?是: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失败:EACCES(权限被拒绝)放置日志的假值是my System.out.println(mFolder.mkdir());输出。使用System.out.println(mFolder.mkdirs());它返回falsetoo@Imrik在其他设备上尝试,不知道这是否有帮助,但
重建
您的项目
// 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);
}