gpt4 book ai didi

写入文件时 Android 权限被拒绝

转载 作者:行者123 更新时间:2023-11-29 15:51:26 24 4
gpt4 key购买 nike

我正在尝试在内部存储中创建一个文件,当我说内部存储不是为了保存安全数据,而是为了保存一些图像或公共(public)文件时,我尝试了一切,但它给我错误,我没有'不知道为什么 D:

代码:

File file = new File(Environment.getDataDirectory() + File.separator
+ "test1111111.txt");
Log.e("ERROR", Environment.getDataDirectory() + File.separator
+ "testfile.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data1 = {
1, 1, 0, 0
};
// write the bytes in file
if (file.exists())
{
OutputStream fo = null;
try {
fo = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fo.write(data1);
} catch (IOException e) {
e.printStackTrace();
}
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("file created: " + file);

}

错误:

04-24 03:41:22.704    7797-7797/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-24 03:41:22.744 7797-7797/com.test.test E/ERROR﹕ /data/testfile.txt
04-24 03:41:22.744 7797-7797/com.test.test W/System.err﹕ java.io.IOException: open failed: EACCES (Permission denied)
04-24 03:41:22.744 7797-7797/com.test.test W/System.err﹕ at java.io.File.createNewFile(File.java:940)

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>

在模拟器中工作正常,但当我尝试使用我的手机时出现错误。模拟器: enter image description here

设备:它给我错误。

已编辑

最佳答案

data目录是系统的目录,永远没有写权限。

例如尝试:

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "testfile.txt");

看这个: Data directory has no read/write permission in Android

关于写入文件时 Android 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841614/

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