Permission denied: File created in .../files

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:


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:


Can anyone help here?


Asked by: Julian755 | Posted: 20-01-2022

Answer 1

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

Answered by: Carlos770 | Posted: 21-02-2022

Answer 2

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.

Answered by: Aldus465 | Posted: 21-02-2022

Answer 3

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.

Answered by: Connie593 | Posted: 21-02-2022

Answer 4

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);
} catch (IOException e) {
    // log error

Answered by: Richard325 | Posted: 21-02-2022

Answer 5

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!

Answered by: Grace312 | Posted: 21-02-2022

Answer 6

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

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), 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,

Answered by: Kelvin456 | Posted: 21-02-2022

Similar questions

java - Permission to write to the SD card

I would like my app to archive the application DB to the SD card. In my code I check if the directory canWrite() exists, and if not, throw an IOException. In this particular instance, I am trying to copy the db file to the root directory on the SD card, but it's throwing an IOException. How can I change the permission on a folder/file to be able to write to it?

git - Why do I get a Windows file permission error (IO Error 13) with Android SDK when writing to files starting with a dot?

Running Titanium Appcelerator 0.8.1 on a Windows XP Virtual Machine, with Android SDK 2.1 When running build/install app, getting the following error (last line broken for display here): [TRACE] f = open(os.path.join(dest, dest_file), "w") [TRACE] IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\firstname.surname\\Desktop\\MyApp\\build\\android\\.classpath' Rem...

android - ADB pull command permission

I do have a question in using ADB command in console. I know that user doesn't have a permission to access /system in shell without root permission as a default. But when I execute "adb pull /system/app /myApps" in a window console, not shell mode, it works properly. I mean, all apk files is copied into my desktop location without any problem. Is it normal case? I got a root permission using exploit before. So, I'm...

android - Which permission do I have to add to the manifest to get gps sensor readings?

Which permission needs my application to get access to the location of the user on Android?

android - Permission to read another app's data?

Is there a permission to allow one app to read the (private) data/data//files/... files of another application? If not, how do backup programs like MyBackup work? C

sqlite - sqlite3 permission denied android

I'm trying to access the database of the application I'm developping directly on my Nexus, but I get a "permission denied" when I tried to execute the "sqlite3" command. I also tried to start the adb in root mod, but again, permission denied on the device... I guess I will have to do that with the emulator but I have a lot of data to load and it would have been 10 times faster with the phone on Wifi than the emulator... Un...

Is it possible to change the permission on files in an Android app's internal storage?

I downloaded a file with an app and stored it inside of its internal storage, but the file is set with -rw------- and I want to change those permissions so is it possible? I know that using external storage is an option but I want to know if I can do it with internal storage too. Thanks for the help. Edit: If it turns out that I can't change the permission is there some shared region of internal storag...

android wifi permission

I don't understand why i need to add WAKE_LOCK permission to the application manifest when I toggle wifi with setWifiEnabled... Any idea ?

Why does the SQLite 3 command using the Android ADB shell return "permission denied"?

Specifically, I was trying to use the sqlite3 command with the ADB shell to run some queries on the database of the Android application I'm building. I kept getting "sqlite3: permission denied". I'm developing on a Nexus One that I purchased from Google. Does my phone need to be rooted or something? $ sqlite3 /data/data/ sqlite3 /data/data/com.moodm...

Android: Force closing for lack of permission which is located in the manifest

I'm trying to delete a bookmark using contentResolver.delete() and I get force close for missing permission "" but it's in the manifest... this is in the manifest (outside &lt;application&gt;&lt;/application&gt;) &lt;uses-permission android:name=""...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android