I am trying to save a csv file to my phone and my target SDK level is 26 so I tried to check run time permissions while running my app. Even though I gave the necessary permissions in manifest file, in my activity checking this permissions return false. How can I fix this?
Start of the manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.something">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Related part of activity is below
public void saveToExcel() throws IOException {
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "AnalysisData.csv";
String filePath = baseDir + File.separator + fileName;
File file = new File(filePath);
CSVWriter writer = null;
String[] data = {"Hold Time", "Down-Down Time", "Up-Down Time"};
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
if (file.exists() && !file.isDirectory()) {
FileWriter mFileWriter = null;
mFileWriter = new FileWriter(filePath, true);
writer = new CSVWriter(mFileWriter);
Log.d(TAG, "saveToExcel: exist");
} else {
writer = new CSVWriter(new FileWriter(filePath));
Log.d(TAG, "saveToExcel:do not exist");
}
writer.writeNext(data);
writer.close();
} else {
Log.d(TAG, "saveToExcel: permission not granted");
}
}