Android 权限被拒绝:在 .../files 中创建的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2022256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permission denied: File created in .../files
提问by Tsimmi
I'm creating a file in data/data/myPackage/files/ :
我正在 data/data/myPackage/files/ 中创建一个文件:
file = new File( getFilesDir() + "/file.txt");
I'm absolutely sure that the file is created.
Right after its creation I call:
我绝对确定该文件已创建。
在它创建后,我立即调用:
file.canWrite();
and the result is true.
结果是真的。
When I try to use that file
I get: "Permission Denied".
In Eclipse, in DDMS, this file permissions are like:
当我尝试使用该文件时,
我得到:“权限被拒绝”。
在 Eclipse 中,在 DDMS 中,此文件权限类似于:
-rw-------
Can anyone help here?
有人可以帮忙吗?
Thanks
谢谢
回答by Neelesh Gajender
Ensure you have the correct permissions to write to the file. In your manifest file, include this line
确保您具有写入文件的正确权限。在您的清单文件中,包括这一行
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
回答by Christopher Orr
Easiest way to open the file (and create it at the same time) would be to use the openFileOutput("file.txt", MODE_PRIVATE)
method.
打开文件(并同时创建它)的最简单方法是使用该openFileOutput("file.txt", MODE_PRIVATE)
方法。
This ensures that the file is only readable by your application (the same as you've have at the moment), plus it returns you a FileOutputStream
so you can start writing to it.
这确保该文件只能由您的应用程序读取(与您目前所拥有的相同),而且它会返回 aFileOutputStream
以便您可以开始写入它。
回答by Dave Webb
The permissions look correct to me. Each application runs as a different user so only has access to it's own files.
权限对我来说是正确的。每个应用程序都以不同的用户身份运行,因此只能访问它自己的文件。
When you say "I try to use that file" what do you mean? Can you post the code for that as I think that is where the problem lies.
当您说“我尝试使用该文件”时,您是什么意思?您能否发布该代码,因为我认为这就是问题所在。
回答by fupsduck
If you know the file is being created using File() and you don't want to use openFileOutput() then try this:
如果您知道文件是使用 File() 创建的,并且您不想使用 openFileOutput() 然后试试这个:
FileWriter fWriter;
try {
fWriter = new FileWriter(file, true);
fWriter.write("hello");
fWriter.close();
} catch (IOException e) {
// log error
}
回答by Mark Eugene
I have been working on the problem of nullpointerexception, file not found, for a few hours. I have moved from the emulator to an actual device. In the emulator the following worked:
几个小时以来,我一直在研究 nullpointerexception,找不到文件的问题。我已从模拟器转移到实际设备。在模拟器中,以下工作:
BufferedWriter osw = new BufferedWriter(new FileWriter(path));
When I went directly to the device though, I got the exception.
Neelesh Gajender has hit the answer perfectly. Adding: :
但是,当我直接进入设备时,出现了异常。
Neelesh Gajender 完美地找到了答案。添加::
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in the android manifest solves this problem completely. I am grateful to Neelesh and Kudos!
在 android manifest 中完全解决了这个问题。我很感谢 Neelesh 和 Kudos!
回答by Tsimmi
I've found that for the writing on '/data/data/myPackage/files/',
permissions need to be set.
我发现对于写入“/data/data/myPackage/files/”,
需要设置权限。
There was MODE_WORLD_WRITEABLE,
that was the one with interest for this case,
but even with this permission set, I still got error.
有 MODE_WORLD_WRITEABLE,
这是对这种情况感兴趣的那个,
但即使设置了这个权限,我仍然遇到错误。
I thought: "maybe what I need is an EXECUTE permission...".
There is no such permission in the API
but even though I tried to put a 3 in the mode parameter
(MODE_WORLD_WRITEABLE is = 2),
and...it worked!
我想:“也许我需要的是 EXECUTE 权限......”。
API 中没有这样的权限,
但即使我试图在模式参数中输入 3
(MODE_WORLD_WRITEABLE 是 = 2),
而且......它起作用了!
But when I write to the sd card with that permission set,
I get an error as well,
so I write differently to the two folders.
但是,当我使用该权限集写入 SD 卡时,
我也会收到错误消息,
因此我对这两个文件夹的写入方式不同。
int permission = 3;
if (whereToWrite == WRITE_TO_DATA_FOLDER) {
FileOutputStream fos = openFileOutput(file.getName(), permission);
buffOutStream = new BufferedOutputStream(fos);
} else if (whereToWrite == WRITE_TO_SD_CARD){
buffOutStream = new BufferedOutputStream(new FileOutputStream(file));
}
And by the way,
MODE_WORLD_WRITEABLE is wrong,
MODE_WORLD_WRITABLE is right.
顺便说一句,
MODE_WORLD_WRITEABLE 是错误的,
MODE_WORLD_WRITABLE 是正确的。