tags:

views:

426

answers:

4

Hi!

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 are like:

-rw-------

Can anyone help here?

Thanks

+2  A: 

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.

Dave Webb
When I say "I try to use that file", I mean I want to install it.In fact, I'm downloading an .apk and putting all of its content in this file. Due to memory space, I have to check if it is possible to create this file in the sd card. If its not, I have to create it in the '/data/data/myPackage/files/' folder.
Tsimmi
+5  A: 

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.

Christopher
NB. openFileOutput and MODE_PRIVATE are on the Context object.
teedyay
+1  A: 

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
}
fupsduck
A: 

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.

Tsimmi