title: android mediaplayer file not found
body: <p>I want to play a music file the user chose before. Choosing the file seems to work (I get a path that looks reasonable) and getting the permission works, too, but the media player throws FileNotFound.
  Since I chose the file 10 seconds earlier it has to exist.<br />
  The error occurs in the &quot;playMusic&quot; function<br />
  My Code is:</p>
  <pre><code>public class Settings extends Fragment {
  
      View mView;
      final int MY_STORAGE_PERMISSION_CODE = 101;
  
      public Settings() {}
  
     public static Settings newInstance(String param1, String param2) {
          Settings fragment = new Settings();
          Bundle args = new Bundle();
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_settings, container, false);
          mView = view;
          String musicFileFromDatabase = DatabaseInteractor.getDatabaseInteractor(view.getContext()).
                  getSetting(DatabaseInteractor.SETTING_MUSIC_FILE);  //gets saved path to music file from database
  
          view.findViewById(R.id.musicFileLabel).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent;
                  intent = new Intent();
                  intent.setAction(Intent.ACTION_GET_CONTENT);
                  intent.setType(&quot;audio/mpeg&quot;);
                  startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), 1);
              }
          });
  
          view.findViewById(R.id.button_play_music).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(DatabaseInteractor.getDatabaseInteractor(getContext()).getSetting(DatabaseInteractor.SETTING_MUSIC_FILE).equals(&quot;&quot;)) {
                      Toast.makeText(getContext(), R.string.select_a_file_first, Toast.LENGTH_LONG).show();
                      return;
                  }
                  if(ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
                          Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                      requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE },MY_STORAGE_PERMISSION_CODE);
                  } else {
                      playMusic();
                  }
              }
          });
          return view;
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == 1 &amp;&amp; resultCode == Activity.RESULT_OK) {
              if ((data != null) &amp;&amp; (data.getData() != null)) {
                  Uri audioFileUri = data.getData();
                  String path = audioFileUri.getPath();
                  //put the path as String into database
                  DatabaseInteractor.getDatabaseInteractor(this.getContext()).
                          updateSetting(DatabaseInteractor.SETTING_MUSIC_FILE, path);
                  Navigation.findNavController(mView).navigate(SettingsDirections.actionSettingsSelf());
              }
          }
      }
      @Override
      public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
          super.onRequestPermissionsResult(requestCode,permissions,grantResults);
          if (requestCode == MY_STORAGE_PERMISSION_CODE) {
              if (grantResults.length &gt; 0
                      &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  playMusic();
              }
              else {
                  Toast.makeText(getContext(),
                          R.string.app_needs_permission,
                          Toast.LENGTH_LONG)
                          .show();
              }
          }
      }
      private void playMusic() {
          String filePath = &quot;file:///&quot; + DatabaseInteractor.getDatabaseInteractor(getContext()).
                  getSetting(DatabaseInteractor.SETTING_MUSIC_FILE);
          Uri uri= Uri.parse(filePath);
  
          MediaPlayer mediaPlayer = new MediaPlayer();
          mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
          try {
              mediaPlayer.setDataSource(getActivity().getApplicationContext(), uri);
              mediaPlayer.prepare();
              mediaPlayer.start();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>My stacktrace is:</p>
  <pre><code>W/System.err: java.io.FileNotFoundException: /external/audio/media/167714: open failed: ENOENT (No such file or directory)
  W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:496)
  W/System.err:     at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:159)
          at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1264)
          at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1208)
          at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1173)
          at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1103)
  W/System.err:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1049)
          at de.annikaknepper.archery.Settings.playMusic(Settings.java:130)
          at de.annikaknepper.archery.Settings.access$000(Settings.java:34)
          at de.annikaknepper.archery.Settings$2.onClick(Settings.java:85)
          at android.view.View.performClick(View.java:7192)
          at android.view.View.performClickInternal(View.java:7166)
  W/System.err:     at android.view.View.access$3500(View.java:824)
          at android.view.View$PerformClick.run(View.java:27592)
          at android.os.Handler.handleCallback(Handler.java:888)
          at android.os.Handler.dispatchMessage(Handler.java:100)
  W/System.err:     at android.os.Looper.loop(Looper.java:213)
          at android.app.ActivityThread.main(ActivityThread.java:8178)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
  W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
  W/System.err:     at libcore.io.Linux.open(Native Method)
          at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
          at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
          at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
          at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8046)
          at libcore.io.IoBridge.open(IoBridge.java:482)
  W/System.err:   ... 20 more
  
  </code></pre>