简体   繁体   English

权限被拒绝:在... /文件中创建的文件

[英]Permission denied: File created in …/files

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

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" />.

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. 这确保了文件只能由您的应用程序读取(与您目前的相同),并且它会返回FileOutputStream以便您可以开始写入它。

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. 你可以发布代码吗,因为我认为这就是问题所在。

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
}

I have been working on the problem of nullpointerexception, file not found, for a few hours. 我一直在研究nullpointerexception的问题,找不到文件几个小时。 I have moved from the emulator to an actual device. 我已经从模拟器移动到实际设备。 In the emulator the following worked: 在模拟器中,以下工作:

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. Neelesh Gajender完美地回答了问题。 Adding: : 添加::

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

in the android manifest solves this problem completely. 在android清单中完全解决了这个问题。 I am grateful to Neelesh and Kudos! 我很感谢Neelesh和Kudos!

I've found that for the writing on '/data/data/myPackage/files/', 我发现在'/ data / data / myPackage / files /'上写,
permissions need to be set. 需要设置权限。

There was MODE_WORLD_WRITEABLE, 有MODE_WORLD_WRITEABLE,
that was the one with interest for this case, 这是对这个案子感兴趣的人,
but even with this permission set, I still got error. 但即使有这个权限集,我仍然有错误。

I thought: "maybe what I need is an EXECUTE permission...". 我想:“也许我需要的是EXECUTE许可......”。
There is no such permission in the API API中没有此类权限
but even though I tried to put a 3 in the mode parameter 但即使我试图在模式参数中加入3
(MODE_WORLD_WRITEABLE is = 2), (MODE_WORLD_WRITEABLE = 2),
and...it worked! 并且...它工作了!

But when I write to the sd card with that permission set, 但是当我使用该权限集写入SD卡时,
I get an error as well, 我也得到一个错误,
so I write differently to the two folders. 所以我对两个文件夹的写法不同。

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_WRITEABLE错了,
MODE_WORLD_WRITABLE is right. MODE_WORLD_WRITABLE是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM