RunTime Permission Not Granted When It Exists In Manifest(Android) [duplicate]
RunTime Permission Not Granted When It Exists In Manifest(Android) [duplicate]
This question already has an answer here:
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");
}
}
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Check this stackoverflow.com/questions/33162152/…
– Ankit Dubey
Jul 2 at 9:07
1 Answer
1
From Android 6 and higher, you need to ask runtime permissions: https://developer.android.com/training/permissions/requesting
In your case, you need to ask runtime permissions for WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
READ_EXTERNAL_STORAGE
checking is not enough ... you should also ask for permissions - feel free to search similar question here on SO or just take a guide on official android dev website
– Selvin
Jul 2 at 9:05