Data Directory Has No Read/Write Permission in Android

Data directory has no read/write permission in Android

You shouldn't be looking at the Data Directory. This is a system directory in the phone's storage - usually /data - and your application will never have permission to write to it.

The directory your application should write files to is returned by the Context.getFilesDir() method. It will be something like /data/data/com.yourdomain.YourApp/files.

If you want to write to a file in the phone's storage use the Context.openFileOutput() method.

If you want the path to the SDCard then use Environment.getExternalStorageDirectory() method. To write to the SDCard you'll need to give your application the appropriate permissions by adding the following to your Manifest:

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

If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState() method.

If you're storing small files to do with your application then these can go into the phone's storage and not the SD Card, so use the Context.openFileOutput() and Context.openFileInput() methods.

So in your code consider something like:

OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));

Android Permission Denied when Writing files

Data directory is a system's directory, you can never have the permission to write in it.

Try for example :

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "testfile.txt");

See this :
Data directory has no read/write permission in Android

Cannot READ/WRITE on External Storage, even having given Permissions

you need to explicitly ask the user to grant said permissions. else it wont work in newer android vesions.try adding this in onCreate

ActivityCompat.requestPermissions(MainActivity.this, new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE},111);

And also

If you are TARGETING Android Q also Environment.getExternalStorageDirectory() no longer leading to an accessible folder, which i think is actually your problem. it is deprecated in favor of Context.getExternalFilesDir(null) which returns a file. that is worth a try as your apps external storage should be something like
"/storage/emulated/0/Android/data/com.example.android.YourApp/" and on that one you should have read and write access

Sample Image

Why don't I have permission to write to app dir on external storage?

I've learned more about this issue, and it's different enough from CommonsWare's answer that I think it's worth a new answer.

  • What made the difference for my original scenario was the SD cards: If a card already had a /Android/data/com.example.myapp folder on it that was not created on this phone, then the app might have trouble getting permission to write to that folder. Whereas if the folder did not exist, the app could create it, and write to it. At least in KitKat and later.

    • This is supported by something explained in this article:

      ... starting with API Level 19 [KitKat], READ_EXTERNAL_STORAGE was no longer required to access files located on external storage – provided the data folder created by the FUSE daemon matches the app’s package name. FUSE would handle synthesizing the owner, group, and modes of files on external storage when an application is installed [emphasis added].

    • So this confirms the guess that the problem arose because I created the app's data folder manually, instead of letting Android set it up as it needed. Future research: Instead of looking only to app permissions like WRITE_EXTERNAL_STORAGE, also check the user, group, and mode settings of the app's data folder on the filesystem: both when created manually, and when created by app installation. Compare & contrast! Maybe this will provide enough information to allow the app's data folder to be created manually and still work. Keep in mind that there is a wrapper / emulation layer over the FAT32 filesystem of the SD card, so we need to consider both filesystem layers.
  • In a later scenario, I found that it's necessary for the app to call context.getExternalFilesDirs(null) in order for the /Android/data/com.example.myapp folder to be created on the SD card. (At least, on Android 5.1 Lollipop and later. Need to test that on KitKat.)

Read and Write in data folder in android app

Your app only has file access to your own directory, ie /data/data/your.package.name.

EDIT: It is now evident that the OP is trying to make an app that accesses files outside of its own data directory on a rooted device. This makes this an entirely different question. As far as I know, it's not possible to directly make an app run as root. What you can do is to run su as a shell command, followed by whatever shell commands you need to run as root. There are several posts about how to do this, for example ANDROID: How to gain root access in an Android application?

This means you might have to perform all your root access operations using shell commands rather than using normal Android API:s. However, if all you need to do is access a certain file outside your own data directory, perhaps you could accomplish this by using chown, chmod etc in a rooted shell to change the permission of the file you are trying to read so that your app's UID can access it (every app is automatically assigned a unique uid such as "u0_a42"). Then you should be able to access it using normal Java file operations.

Perhaps you don't even need to actually change the owning user of the file to your app UID, but could use some existing group that your app already belongs to, such as the one controlling access to the sd card (this is speculation). You might want to give this a try:

su
chown root:sdcard_r /data/path/to/file

Provided that your app holds android.permission.READ_EXTERNAL_STORAGE I reckon you should now be able to access the file. You could probably even use some other group that isn't mapped to any permission.

No write permissions on API 23 but all needed permissions granted

You have not been able to write to arbitrary locations on removable storage since Android 4.4. Your permissions are for external storage, which is not removable storage.

READ or WRITE EXTERNAL STORAGE permission : WHY NO ERROR?

Here I found answer in documentation:

Beginning with Android 4.4 (API level 19), reading or writing files
in your app's private external storage directory — accessed using
getExternalFilesDir() — does not require the
READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions.

Its not required for my project because my minimum API level is 19.

So if your app supports Android 4.3 (API level 18) and lower, and
you want to access only the private external storage directory, you
should declare that the permission be requested only on the lower
versions of Android by adding the maxSdkVersion attribute:

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

Thank you.



Related Topics



Leave a reply



Submit