Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Creating a CSV file in Android 11 Return Error "java.io.FileNotFoundException: EPERM (Operation not permitted)"

Ask Question

Hello I am trying to create a csv file in my android application. My code works when I try to run it on android 10 below. But I cant seem to find a problem why I cant do it on devices that are Android 11 already.

Here is my Manifest Permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
        android:requestLegacyExternalStorage="true">

Here is my code where I create a folder inside the download folder

@NonNull
public static File getStorageDirectory() {
    if (Environment.getExternalStorageState() == null) {
        File f = new File(Environment.getDataDirectory().getAbsolutePath() + "/Download/(Project Name)/");
        if(!f.exists())
            f.mkdirs();
        return f;
    } else {
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/(Project Name)/");
        if(!f.exists())
            f.mkdirs();
        return f;

Here is my code on how I create my csv file

File baseDir = Utility.getStorageDirectory();
String fileName = pollutant.getStationName();
String date = Utility.convertDate(new Date().getTime(), "dd-MM-yyyy HH:mm:ss");
File csvFile = new File(baseDir, fileName + "(1hr - "+pollutant.getPollutantName()+")(" + date + ").csv");
FileWriter writer;
if(csvFile.exists() && !csvFile.isDirectory()) {
    writer = new FileWriter(csvFile , true);
} else {
    writer = new FileWriter(csvFile);

I am already creating a folder in the download folder in android 11 problem is when I am trying to do the create csv part program return a

java.io.FileNotFoundException: ... open failed: EPERM (Operation not permitted)

I am really having a hard time to fix my problem for devices with android 11

Long press on your app, click on ```info`` go to the app permission you will get what I mean – 钟智强 Mar 10, 2021 at 3:40 I have a similar issue with the file name "?.txt". I can store it locally in my app (SFTP Server s0 v1) directory "/data/data/ch.becke.sftp_server__s0_v1/files" but when I store it on the external storage directory "/storage/emulated/0/Download" I get this error "android.system.ErrnoException: open failed: EPERM (Operation not permitted)". Other file names are no problem. Do you have evidence which file name characters are allowed in which Android Version? (Actually Android is based on Linux and on Linux I can have ":" and "?" in my file name without issues ...) – becke-ch Sep 15, 2022 at 16:31 Do you know an an article/web-site where it is listed/explained which characters are allowed and which ones are not? BUT this does not explain why using my Android SFTP Server s0 v1 App I can upload and store files containing special characters ":", "?", ... to my app directory "/data/data/ch.becke.sftp_server__s0_v1/files" but not on the external storage directory e.g. "/storage/emulated/0/Download". – becke-ch Sep 16, 2022 at 21:39

I believe that this a permission issue. In Order to Write into files you need to allow it. Here is example of the code:

public void onPermission() 
// Permision can add more at your convinient
    if ((ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) !=
        PackageManager.PERMISSION_GRANTED) 
        ActivityCompat.requestPermissions(
            this,
            new String[] 
                Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.CAMERA,
                    Manifest.permission.CALL_PHONE

Usage :

onPermission(); at onStart() or in the onAcitivty().

Alternatively you can use a library as well ,

Reference : https://github.com/Karumi/Dexter

For External Storage: Add attribute in your app's AndroidManifest.xml file inside the application tag:

android:requestLegacyExternalStorage="true"

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:requestLegacyExternalStorage="true">
</application>

For More Reference : https://stackoverflow.com/a/61406983/10758321

yes I already have the android:requestLegacyExternalStorage="true" in my manifest file. It is still returning the same error. – Chadson Mar 10, 2021 at 3:56 Yes I have. I really can't figure out what is missing in my project for it to run in Android 11 it works fine in Android 10 below.... – Chadson Mar 10, 2021 at 3:59

I have a similar behavior in my “SFTP Server s0 v1” app and the problem respective the allowed characters in file- and directory-names is related to the underlying file system – see Comparison of file systems . You can check which file system is mounted to which directory by calling adb shell mount. For example creating a file in your application home directory (e.g. /data/data/ch.becke.sftp_server__s0_v1/files) is no issue because the /data directory is mounted as ext4 (/dev/block/dm-5 on /data type ext4 ...) which allows almost all characters in file names. Whereas the external storage directory (e.g. /storage/emulated/0 or /storage/140E-3C1A) is mounted as vfat or fuse which only allows a limited set of characters in file names. (in my app I alternatively also support managing files using scoped storage but I personally think this is even worse because it auto replaces all special characters with an underscore character _).

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.