title: Download Manager Android Permissions error: write external Storage
body: <p>I programmed an Android App that downloads a sample PDF file to the Download directory using DownloadManager. Here's the code:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_vertretungsplan);
      Button dlbutton = (Button) findViewById(R.id.buttondownload);
      final Context c = this;
      dlbutton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myurl));
              request.setTitle("Vertretungsplan");
              request.setDescription("wird heruntergeladen");
              request.allowScanningByMediaScanner();
              request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
              String filename = URLUtil.guessFileName(myurl, null, MimeTypeMap.getFileExtensionFromUrl(myurl));
              request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
              DownloadManager manager = (DownloadManager) c.getSystemService(Context.DOWNLOAD_SERVICE);
              manager.enqueue(request);
          }
      });
  }
  </code></pre>
  
  <p>Now, I tried to debug-run it from Android Studio on my Xperia Z3 Android 6.0. 
  As I clicked on the download button, this error occurs:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: com.example.markwitt.schul_app, PID: 29297
                java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/hrpsampletable.pdf: Neither user 10047 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
                    at android.os.Parcel.readException(Parcel.java:1627)
                    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
                    at android.content.ContentResolver.insert(ContentResolver.java:1240)
                    at android.app.DownloadManager.enqueue(DownloadManager.java:946)
                    at com.example.markwitt.schul_app.Vertretungsplan$1.onClick(Vertretungsplan.java:59)
                    at android.view.View.performClick(View.java:5280)
                    at android.view.View$PerformClick.run(View.java:21239)
                    at android.os.Handler.handleCallback(Handler.java:739)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:224)
                    at android.app.ActivityThread.main(ActivityThread.java:5526)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'</p>
  
  <p>(on the third line) it displays me that it has no write permission on external storage.</p>
  
  <p>But my AndroidManifest is set up correctly:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.markwitt.schul_app"&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;application
      android:launchMode="singleTop"
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  
      &lt;activity android:name=".Stundenplan"&gt;
  
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name=".Vertretungsplan"&gt;&lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>What do I need to do?</p>