title: Android 11: Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, Documents]
body: <p>I am trying to <strong>create PDF file</strong> from base64 string. Because of <em><a href="https://developer.android.com/about/versions/11/privacy/storage" rel="nofollow noreferrer">Storage Update in Android 11</a></em>, I have to change my code but I'm getting following error in Android 11 devices:</p>
  <pre><code>java.lang.IllegalArgumentException: Primary directory (invalid) not allowed for content://media/external/file; allowed directories are [Download, Documents]
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:549)
     at android.content.ContentResolver.insert(ContentResolver.java:2149)
     at android.content.ContentResolver.insert(ContentResolver.java:2111)
  </code></pre>
  <p>This code create a PDF file and save it into folder.</p>
  <pre><code>public static void createPDF(Context mContext, String fileName, String base64) {
      try {
          String folderPath;
          File dwldsPath;
  
          if (Build.VERSION.SDK_INT &gt; Build.VERSION_CODES.Q) {
              folderPath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + File.separator + &quot;appFolderName&quot;;
              dwldsPath = new File(folderPath + &quot;/&quot; + fileName);
  
              File folder = new File(folderPath);
              folder.mkdirs();
  
              ContentValues values = new ContentValues();
              values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); // file name
              values.put(MediaStore.MediaColumns.MIME_TYPE, &quot;application/pdf&quot;); // file extension, will automatically add to file
              values.put(MediaStore.DownloadColumns.RELATIVE_PATH, folderPath); // end &quot;/&quot; is not mandatory
              Uri uriFile = mContext.getContentResolver().insert(MediaStore.Files.getContentUri(&quot;external&quot;), values); // important!
              OutputStream outputStream = mContext.getContentResolver().openOutputStream(uriFile);
              outputStream.write(Base64.decode(base64, 0));
              outputStream.close();
          } else {
              folderPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + File.separator + &quot;appFolderName&quot;;
              dwldsPath = new File(folderPath + &quot;/&quot; + fileName);
  
              File folder = new File(folderPath);
              folder.mkdirs();
  
              FileOutputStream os = new FileOutputStream(dwldsPath, false);
              os.write(Base64.decode(base64, 0));
              os.flush();
              os.close();
          }
  
          openPDF(mContext, dwldsPath);
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ActivityNotFoundException e) {
          Toast.makeText(mContext, &quot;No PDF Viewer Installed&quot;, Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  <p>This code is working for opening file</p>
  <pre><code>  public static void openPDF(Context mContext, File dwldsPath) {
          Intent intentUrl = new Intent(Intent.ACTION_VIEW);
          Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + &quot;.provider&quot;, dwldsPath);
          intentUrl.setDataAndType(uri, &quot;application/pdf&quot;);
          intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intentUrl.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          mContext.startActivity(intentUrl);
      }
  </code></pre>
  <p>In addition to this error, <strong>folder.mkdirs() returns false</strong> in Android 11. Here is provider_paths.xml and defined in AndroidManifest.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;paths&gt;
      &lt;external-path
          name=&quot;external&quot;
          path=&quot;.&quot; /&gt;
      &lt;external-files-path
          name=&quot;external_files&quot;
          path=&quot;.&quot; /&gt;
      &lt;files-path
          name=&quot;files&quot;
          path=&quot;.&quot; /&gt;
  &lt;/paths&gt;
  </code></pre>
  <p>I google it but I couldn't find any working solution to fix problem. Thanks in advance.</p>