gpt4 book ai didi

java.io.FileNotFoundException :/storage/sdcard/MyApp/questions. txt:打开失败:EACCES(权限被拒绝)

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:40 24 4
gpt4 key购买 nike

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

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

我把我用来保存的代码(这段代码适用于旧的 Eclipse,在新的 Eclipse 中给我异常(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();
}
}

还有我的 list 文件:

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

还有一张我的模拟器详细信息的图片:

enter image description here

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

最佳答案

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

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

关于java.io.FileNotFoundException :/storage/sdcard/MyApp/questions. txt:打开失败:EACCES(权限被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com