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