title: Android (Write on external SD-Card): java.io.IOException: Permission denied
body: <p>I get a "Permission denied" error, when I want to create a file on my external SD-Card (named /storage/B9BE-18A6).</p>
  
  <p>I know that you have to ask for the write permission programmatically since Android M. So I inserted the solution from Arpit Patel (<a href="https://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card">Android 6.0 Marshmallow. Cannot write to SD Card</a>)</p>
  
  <p>I don't know why I still haven't the permissions to do it. 
  Do you guys have another solution that I can create files on my SD-Card?</p>
  
  <p><strong>Code for creating the file</strong></p>
  
  <pre><code>    FloatingActionButton fab_new_file = (FloatingActionButton) rLayoutFrgEmpresas.findViewById(R.id.fab_menu_item_file);
      fab_new_file.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
  
              int permission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
              if (permission != PackageManager.PERMISSION_GRANTED) {
                  Log.v("Permission: " ,"Denied");
  
                  if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                      builder.setMessage("Permission to access the SD-CARD is required")
                              .setTitle("Permission required");
  
                      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  
                          public void onClick(DialogInterface dialog, int id) {
                              Log.i(TAG, "Clicked");
                              makeRequest();
                          }
                      });
  
                      AlertDialog dialog = builder.create();
                      dialog.show();
  
                  } else {
                      makeRequest();
                  }
              }else{
                  Log.v("Permission: " ,"Granted");
                  File file = new File(textView_currentPath.getText() + "/" + "testfile.txt");
                  Log.v("filepatch: ", ""+file);
                  if (!file.exists()) {
                      Log.v("Does "+file+" exists?", "No");
                      try {
                          file.createNewFile();
                          getFilesFromDir(textView_currentPath.getText() + "", textView_currentPath.getText() + "");
                          Log.v("File "+file,"has been created!");
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
      });
  </code></pre>
  
  <p><strong>LogCat</strong></p>
  
  <pre><code>04-11 16:31:40.709 23404-23404/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 0
  04-11 16:31:40.773 23404-23404/spicysoftware.com.phonemanager D/ViewRootImpl@ab22b33[MainActivity]: ViewPostImeInputStage processPointer 1
  04-11 16:31:40.783 23404-23404/spicysoftware.com.phonemanager V/Permission:: Granted
  04-11 16:31:40.783 23404-23404/spicysoftware.com.phonemanager V/filepatch:: /storage/B9BE-18A6/testfile.txt
  04-11 16:31:40.783 23404-23404/spicysoftware.com.phonemanager V/Does /storage/B9BE-18A6/testfile.txt exists?: No
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err: java.io.IOException: Permission denied
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.io.File.createNewFile(File.java:948)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at spicysoftware.com.phonemanager.StorageFragment$2.onClick(StorageFragment.java:187)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.view.View.performClick(View.java:6207)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.view.View$PerformClick.run(View.java:23639)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.os.Looper.loop(Looper.java:154)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6688)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
  04-11 16:31:40.784 23404-23404/spicysoftware.com.phonemanager W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
  </code></pre>
  
  <p><strong>Solution</strong>
  Thanks to @greenapps for bringing me on the right way.
  Solution is very well documentated at <a href="https://developer.android.com/guide/topics/providers/document-provider.html" rel="nofollow noreferrer">https://developer.android.com/guide/topics/providers/document-provider.html</a></p>
  
  <pre><code>        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
          intent.addCategory(Intent.CATEGORY_OPENABLE);
  
          String mimeType = "text/plain";
          String filename = "file"+System.currentTimeMillis() + ".txt";
          intent.setType(mimeType);
          intent.putExtra(Intent.EXTRA_TITLE, filename);
          startActivityForResult(intent, CREATE_REQUEST_CODE);
  </code></pre>