Why can't I save file in external storage or memory card in android : I would like to create a backup file in my memory card but all it does is return a file not found exception. I am specifying the path where the data should be saved. When i choose the Internal storage the file was saved but when i changed it to external storage, it returns me the file not found. 

Here are the Screenshots: 
[enter image description here][1]

     final Preference prefStoragePath = findPreference("key_storage_path");
            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
            String pref_storage_path = settings.getString("set_storage_path",null);
            startingDir = (pref_storage_path!=null)? pref_storage_path : Environment.getExternalStorageDirectory().toString();

     Preference prefBackupManual = findPreference("key_backup_manual");
            final String root = Environment.getExternalStorageDirectory().toString();
            prefBackupManual.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    createBackupData(startingDir);
                    return false;
                }
            });

    private void createBackupData(String dir){
            String filename = "BackupData.txt";
            String data = resultSet().toString();
            try{
                byte[] sha1hash;
                File myFile =  new File(dir,filename);
                sha1hash = data.getBytes("UTF-8");
                String base64 = Base64.encodeToString(sha1hash, Base64.DEFAULT);
                FileOutputStream fos = new FileOutputStream(myFile);
                fos.write(base64.getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "no file", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), "file not saved", Toast.LENGTH_SHORT).show();
            }finally {
                Toast.makeText(getActivity(),"File saved in " + dir + "/" + filename ,Toast.LENGTH_SHORT).show();
            }
        }

  [1]: https://i.stack.imgur.com/BG5xs.png