-1

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");
            }
        }
Sam
  • 31
  • 4
  • 2
    **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 02 '18 at 09:05
  • Check this https://stackoverflow.com/questions/33162152/storage-permission-error-in-marshmallow – Ankit Dubey Jul 02 '18 at 09:07

1 Answers1

0

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

Liar
  • 1,235
  • 1
  • 9
  • 19