[英]Android create file exception EACCES (Permission denied)
I cannot write to the internal storage of an android device or emulator. 我无法写入android设备或仿真器的内部存储。 I am able write to a file in the external storage directory.
我可以写入外部存储目录中的文件。
I am getting this exception EACCES (Permission denied) on the createNewFile call. 我在createNewFile调用中收到此异常EACCES(权限被拒绝)。
WRITE_EXTERNAL_STORAGE permission is in the right place in my manifest WRITE_EXTERNAL_STORAGE权限在我的清单中正确的位置
public static Boolean writeFile(String filename, String data)
{
try
{
File file = new File(filename);
// If file does not exists, then create it
if (!file.exists())
{
Log.d("MyLog","file doesn't exist, creating");
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
//Log.d("Success","Success");
return true;
}
catch (IOException e)
{
e.printStackTrace();
Log.d("MyLog","writeFile fail, error:"+e.getMessage());
return false;
}
}
I have tried numerous paths: 我尝试了许多路径:
filename=Environment.getRootDirectory()+"/"+"test.dat";
filename=Environment.getDataDirectory()+"/"+"test.dat";
filename="data/data/com.android.emsi/testnew.txt";
the only thing that will work is: filename=Environment.getExternalStorageDirectory()+"/"+"test.dat"; 唯一起作用的是:filename = Environment.getExternalStorageDirectory()+“ /” +“ test.dat”;
Try this 尝试这个
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.