Permission denied while save a file in Android
<p>I am developing an Android app. In my app, I am saving a bitmap as file. But it is throwing permission denied exception even if I set the right permissions in the manifest file.</p>

<p>This is how I save a bitmap as a file</p>

<pre><code>private void downloadImage(Bitmap bitmap)
    {
        try{
            String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

            if(file.exists()){
                file.delete();
            }

            OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);


            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(file);
            mediaScanIntent.setData(contentUri);
            sendBroadcast(mediaScanIntent);
            Toast.makeText(getBaseContext(),"Image saved to your device",Toast.LENGTH_SHORT).show();
        }
        catch (IOException e)
        {
            Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }
</code></pre>

<p>When I save, it is toasting me error like this.</p>

<p><a href="https://i.stack.imgur.com/auf2W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/auf2W.png" alt="enter image description here"></a></p>

<p>These are all the permissions I set in manifest file.</p>

<pre><code> &lt;uses-permission android:name="android.permission.WAKE_LOCK"/&gt;
    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
    &lt;uses-feature android:name="android.hardware.camera" /&gt;
    &lt;uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /&gt;
    &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
</code></pre>

<p>Why is that error throwing? How can I correct my code? </p>