开发者

Permission denied: File created in .../files

开发者 https://www.devze.com 2022-12-15 22:07 出处:网络
I\'m creating a file in data/data/myPackage/files/ : file = new File( getFilesDir() + \"/file.txt\"); I\'m absolutely sure that the file is created.

I'm creating a file in 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 开发者_JAVA技巧are like:

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

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.


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:

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. 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. Adding: :

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

in the android manifest solves this problem completely. I am grateful to Neelesh and Kudos!


I've found that for the writing on '/data/data/myPackage/files/',
permissions need to be set.

There was 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...".
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!

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消