title: For apps targeting Android11, the requestLegacyExternalStorage flag will be ignored
body: <p>My app is in Play store, today I received a important email that I have to fix this in next update otherwise they will remove my app from playstore . Can anyone tell me how can I fix this ..
  thanks in advance</p>
  <blockquote>
  <p>We've detected that your app contains the requestLegacyExternalStorage
  flag in the manifest file of one or more of your app bundles or APKs.</p>
  <p>Developers with apps on devices running Android 11+ must use scoped
  storage to give users better access control over their device storage.
  To release your app on Android 11 or newer after 5 May, you must
  either:</p>
  <p>Update your app to use more privacy-friendly best practices, such as
  the storage access framework or Media Store API Update your app to
  declare the All files access (MANAGE_EXTERNAL_STORAGE) permission in
  the manifest file, and complete the All files access permission
  declaration in Play Console from 5 May Remove the All files access
  permission from your app entirely For apps targeting Android 11, the
  requestLegacyExternalStorage flag will be ignored. You must use the
  All files access permission to retain broad access.</p>
  <p>Apps requesting access to the All files access permission without a
  permitted use will be removed from Google Play, and you won't be able
  to publish updates.</p>
  </blockquote>
  <pre><code>Manifest.xml
    &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  
          &lt;application
              android:allowBackup=&quot;true&quot;
              android:icon=&quot;@mipmap/ic_launcher&quot;
              android:label=&quot;@string/app_name&quot;
              android:resizeableActivity=&quot;false&quot;
              android:requestLegacyExternalStorage=&quot;true&quot;
              android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
              android:supportsRtl=&quot;true&quot;
              android:theme=&quot;@style/Theme.YMusic&quot;&gt;
              &lt;activity android:name=&quot;com.developer_ashu.ymusic.PlaySong&quot;
                  android:launchMode=&quot;singleTop&quot;
                  android:screenOrientation=&quot;portrait&quot;
                  android:parentActivityName=&quot;com.developer_ashu.ymusic.MainActivity&quot;&gt;
      
              &lt;/activity&gt;
              &lt;activity android:name=&quot;com.developer_ashu.ymusic.MainActivity&quot;
                  android:screenOrientation=&quot;portrait&quot;&gt;
                  &lt;intent-filter&gt;
                      &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
      
                      &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
                  &lt;/intent-filter&gt;
              &lt;/activity&gt;
              &lt;receiver android:name=&quot;.Notification.NotificationReceiver&quot;/&gt;
          &lt;/application&gt;
      
      &lt;/manifest&gt;
  
  
  
  MainActivity.java
  public class MainActivity extends AppCompatActivity {
      ListView listView;
      MediaPlayer mediaPlayer;
     ArrayList&lt;song_info&gt; listSongs = new ArrayList&lt;song_info&gt;();
     private MySongAdapter mySongAdapter ;
      private  String[] itemsAll;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          listView = findViewById(R.id.listView);
  
  
          Dexter.withContext(this)
                  .withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                  .withListener(new PermissionListener() {
                      @Override public void onPermissionGranted(PermissionGrantedResponse response) {
                        loadSongs();
                      }
                      @Override public void onPermissionDenied(PermissionDeniedResponse response) {
  
  
                      }
                      @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                          token.continuePermissionRequest();
                       }
                  }).check();
      }
  
    private void loadSongs(){
          Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + &quot;!=0&quot;;
        Cursor cursor = getContentResolver().query(uri,null,selection,null,null);
        if (cursor != null){
            while(cursor.moveToNext()){
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String author = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
  
                listSongs.add(new song_info(title,author,url));
            }
        }
        mySongAdapter = new MySongAdapter(listSongs);
        listView.setAdapter(mySongAdapter);
    }
  
      private class MySongAdapter extends BaseAdapter {
          ArrayList&lt;song_info&gt; myListSongs;
  
          public MySongAdapter(ArrayList&lt;song_info&gt; myListSongs) {
              this.myListSongs = myListSongs;
          }
  
          @Override
          public int getCount() {
              return myListSongs.size();
          }
  
          @Override
          public Object getItem(int position) {
              return myListSongs.get(position);
          }
  
          @Override
          public long getItemId(int position) {
              return (long) position;
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              TextView textView,textView1;
              Button button;
              LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
              View view = inflater.inflate(R.layout.my_layout,parent,false);
              song_info song = myListSongs.get(position);
              textView = view.findViewById(R.id.textView1);
              textView1 = view.findViewById(R.id.textView2);
              button = view.findViewById(R.id.button);
              textView.setText(song.Title);
              textView1.setText(song.Author);
              button.setOnClickListener(v -&gt; {
                  if ( button.getText().equals(&quot;STOP&quot;)){
                      mediaPlayer.stop();
                      mediaPlayer.release();
                      button.setText(&quot;PLAY&quot;);
                  }
                  else   {
                      mediaPlayer = new MediaPlayer();
                      try {
                          Log.v(&quot;MainActivity.this&quot;,song.songUrl);
                          mediaPlayer.setDataSource(song.songUrl);
                          // After this line it throws an error if I remove requestLegacyStorage from manifest
  //                        Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) -- this is the error
                          mediaPlayer.prepare();
                          mediaPlayer.start();
                          button.setText(&quot;STOP&quot;);
  
                      }catch (Exception e){
                          Toast.makeText(getApplicationContext(), &quot;Error in playingSong&quot;, Toast.LENGTH_SHORT).show();
                      }
                  }
              });
              return view;
          }
      }
  }
  
  
  
  
  song_info.java
  public class song_info {
      String Title = null;
      String Author = null;
      String songUrl = null;
  
      public song_info(String title, String author, String songUrl) {
          Title = title;
          Author = author;
          this.songUrl = songUrl;
      }
  }
  </code></pre>