title: Android: Media files vanish from file system
body: <p>I am stuck with a weird problem. I have an activity with a MediaPlayer that should play a just recorded audio file. At first the mediaplayer is initialized ok and  the file can be played. </p>
  
  <p>When I rotate the screen, the activity is destroyed and then reinitialized for the new orientation. Therefore, I  re-initialize the mediaplayer too.</p>
  
  <p>this works a couple of times, but at some point mediaPlayer.setDataSource() throws a NullPointerException because the file is suddenly gone. Sadly, I haven't seen any other error in the logs.</p>
  
  <p>Here are some Code snippets:</p>
  
  <p>player creation:</p>
  
  <pre><code>/**
   * Creates and initializes the player with the proper file.
   */
  private void createPlayer() {
      synchronized (playerMutex) {
          player = new MediaPlayer();
          player.setLooping(false);
          player.setOnPreparedListener(this);
          player.setOnErrorListener(this);
          player.setOnCompletionListener(this);
      }
      readGreeting();
  }
  </code></pre>
  
  <p>player initialization:</p>
  
  <pre><code>    isPrepared = false;
      try {
          final File file = new File(audioFilename);
  
          in = new FileInputStream(file);
          synchronized (playerMutex) {
              player.setDataSource(in.getFD());
          }
  
          // using a runnable instead of prepareAsync to not accidentally call pause on media player while preparing
          Runnable preparer = new Runnable() {
              @Override
              public void run() {
                  try {
                      synchronized (playerMutex) {
                          if (player != null) {
                              player.prepare();
                          }
                      }
                  } catch (Exception ex) {
                      Log.e(TAG, "Error preparing player for file " + file.getAbsolutePath(), ex);
                  }
              }
          };
          new Thread(preparer).start();
      } catch (Exception ex) {
          btnPlayback.setEnabled(false);
          Log.e(TAG, "Error preparing player", ex);
      } finally {
          if (in != null) {
              try {
                  in.close();
              } catch (IOException e) {
                  Log.e(TAG, "initPlayer: ", e);
              }
          }
      }
  </code></pre>
  
  <p>saving instance state...</p>
  
  <pre><code>@Override
  protected void onPause() {
      synchronized (playerMutex) {
  
          if (isPlaying()) {
              getIntent().putExtra(EXTRA_KEY_SEEK, player.getCurrentPosition());
              pause();
          }
      }
      setAudioModeBackToNormal();
      super.onPause();
  }
  
  private void pause() {
      synchronized (playerMutex) {
          if (isPlaying()) {
              player.pause();
          }
      }
      btnPlayback.setVisibility(View.VISIBLE);
      btnPause.setVisibility(View.GONE);
  }
  
  @Override
  protected void onSaveInstanceState(Bundle outState) {
      final Bundle extras = getIntent().getExtras();
  
      outState.putBundle("extras", extras);
      super.onSaveInstanceState(outState);
  }
  
  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      getIntent().putExtras(savedInstanceState.getBundle("extras"));
  }
  </code></pre>
  
  <p>cleanup:</p>
  
  <pre><code>private void stopPlayerAndFreeResources() {
      synchronized (playerMutex) {
          isPrepared = false;
          if (player != null) {
              player.stop();
              player.release();
              player = null;
          }
      }
      if (in != null) {
          try {
              in.close();
              in = null;
          } catch (IOException e) {
               Log.e(TAG, "Unexpected error", e);
          }
      }
  }
  </code></pre>
  
  <p>Maybe I'm looking at the problem from the wrong angle and it has nothing to do with player. Has anybody ever had issues with disappearing files?</p>