1

in the mainFest it has

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and for newer OS it checks the permission

ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

if it does not return true, it will request the permission

ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 5);

and only when it has the permission it will try to create the file

File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

String sysDownloadPath = directory.getPath();


                try {
                    if (!directory.exists() && !directory.mkdirs()) {
                        //shouldn't happen in here
                        /* Checks if external storage is available for read and write */
                        String state = Environment.getExternalStorageState();
                        String externalStorageAvailable = (Environment.MEDIA_MOUNTED.equals(state)) ?
                                "ext-ST available" :
                                "ext-ST NOT available";                        }
                } catch (Exception e) {  //<=== did not caught exception here
                }

                String fileName = ‘SD_20170404.pdf’;

                String newFilePath = (sysDownloadPath + "/" + fileName);
                File newFile = new File(newFilePath);

                try {
                    newFile.createNewFile();// <=== throws at this one
                } catch (Exception e) {


fatal Exception: java.lang.Exception: open failed: EACCES (Permission    denied), newFile.getPath():/storage/emulated/0/Download/SD_20170404.pdf

It happens (not always but) with OS 5, 6 and 7. any suggestion? thanks!

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • 3
    **NEVER HARDCODE PATHS**. `/storage/emulated/0/` is not the correct directory on all devices. Use methods (`getExternalFilesDir()`, `getExternalCacheDir()`, methods on `Environment`, etc.) to build a valid path for the current device. – CommonsWare Apr 05 '17 at 12:34
  • Show full code please. – tahsinRupam Apr 05 '17 at 12:42
  • thanks, CommonsWare!, the path is copied from the exception just for showing the problem. It is not hard coded in the application. It is doing File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); – lannyf Apr 05 '17 at 12:45
  • Does that Download directory exist? You should add code to check that. `if(!newFile.getParentFile().exists()) return;`. Display a toast too to inform the user. – greenapps Apr 05 '17 at 12:47
  • the path is from File directory = Environment.getExternalStoragePublicDirectory(Environment.DI‌​RECTORY_DOWNLOADS); and just appended the new fileName to it to create a new file. – lannyf Apr 05 '17 at 12:50
  • You already told that. I knew already. Why did you telll that twice? Better react to what i said/asked. – greenapps Apr 05 '17 at 12:51
  • i think you have open file required read permisson @lannyf – Hasmukh Kachhatiya Apr 05 '17 at 12:55
  • `it will try to open the file`. It will try to create a file. – greenapps Apr 05 '17 at 12:56
  • thanks greenapps! is there a case that the Environment.getExternalStoragePublicDirectory(Environment.DI‌​‌​RECTORY_DOWNLOADS)‌ returns the path but the path actually does not exist? And edit the question it is trying to create a new file. – lannyf Apr 05 '17 at 13:10
  • edited the question with more actual code used for building the path. – lannyf Apr 05 '17 at 13:46
  • You still did not answer my question. – greenapps Apr 05 '17 at 15:05
  • `//<=== did not caught exception here`. Does not matter. Your catch block is empty wich is bad. You should put a Toast() in it to inform the user if the exception happens. And a return statement. As now you would continue with the code even if there was a catch. Also the normal e.printStackTrace(); – greenapps Apr 05 '17 at 15:07
  • You should also put a Toast() and a return statement in case the directory cannot be created. You are now continuing with the code flow. – greenapps Apr 05 '17 at 15:09
  • @greenapps, thank you for looking at it. The question is why it still throws after having the permission and confirmed the folder exist. – lannyf Apr 06 '17 at 16:42

0 Answers0