title: Accessing internally stored media files - MediaPlayer on Android
body: <p>I am trying to play a file in internal storage with <code>MediaPlayer</code> and am unable to do so. The initialization sequence fails on the <code>setDataSource</code> part. The code I am using:  </p>
  
  <pre><code>public class HelloMediaPlayer extends Activity {
  
      private MediaPlayer mp = null;
      private Button play;
      private boolean isPrepared = false;
      private String fileName;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_hello_media_player);
  
          //init mediaplayer.
          mp = new MediaPlayer();
  
          mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  
              @Override
              public void onPrepared(MediaPlayer mp) {
                  Log.i(this.toString(), "Mediaplayer prepared.");
                  isPrepared = true;
              }
          });
  
          fileName = getFilesDir().getAbsolutePath() + "/word_recorded.wav"; 
          //fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/word.wav";
          Log.v(this.toString(), "filename = " + fileName);
          File file = new File(fileName);
          String str = file.getName();
          Log.v(this.toString(), "file name = " + str);
          if(file.exists()) {
              if(file.canRead()) {
                  Log.v(this.toString(), fileName + " exists and can be read.");
              } else {
                  Log.e(this.toString(), fileName + " exists but cannot be read.");
                  if(file.setReadable(true, false)) {
                      Log.v(this.toString(), "Changed permissions on " + file.getName());
                  } else {
                      Log.e(this.toString(), "Failed to set permissions either.");
                      Log.e(this.toString(), "Attempting to copy into another file.");
  
                      try {
                          @SuppressWarnings("deprecation")
                          FileOutputStream fos = openFileOutput("word_recorded2.wav", Context.MODE_WORLD_READABLE);
                          InputStream is = openFileInput("word_recorded.wav");
  
                          //copy data.
                          byte[] buffer = new byte[8192];
                          int length = 0;
                          try {
                              while((length = is.read(buffer)) &gt; 0) {
                                  fos.write(buffer, 0, length);
                              }
                              fos.flush();
                              fos.close();
                              is.close();
                          } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          Log.v(this.toString(), "Copied file name.");                        
                      } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
              }
          } else {
              Log.e(this.toString(), fileName + " does not exist.");
          }
  
          try {
              //mp.setDataSource(openFileInput(str).getFD());
              mp.setDataSource(fileName);
          } catch (IllegalArgumentException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (IllegalStateException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (FileNotFoundException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          Log.v(this.toString(), "Data source set to: " + fileName);
          mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
          Log.v(this.toString(), "Audio stream for mp set.");
          try {
              mp.prepare();
          } catch (IllegalStateException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }   
  
          play = (Button) findViewById(R.id.btn);
          play.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  Log.v(this.toString(), "Clicked Play button.");
                  if(isPrepared) {
                      Log.v(this.toString(), "Mediaplayer is prepared.");
                      mp.start();
                  } else {
                      Log.e(this.toString(), "mediaplayer is not prepared.");
                  }
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.hello_media_player, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>The <code>LogCat</code> messages:  </p>
  
  <pre><code>10-27 10:23:31.630: V/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): file name = word_recorded.wav
  10-27 10:23:31.640: E/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav exists but cannot be read.
  10-27 10:23:31.640: E/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): Failed to set permissions either.
  10-27 10:23:31.650: E/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): Attempting to copy into another file.
  10-27 10:23:31.650: W/System.err(19317): java.io.FileNotFoundException: /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav: open failed: EACCES (Permission denied)
  10-27 10:23:31.650: W/System.err(19317):    at libcore.io.IoBridge.open(IoBridge.java:406)
  10-27 10:23:31.650: W/System.err(19317):    at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:78)
  10-27 10:23:31.650: W/System.err(19317):    at android.app.ContextImpl.openFileInput(ContextImpl.java:610)
  10-27 10:23:31.660: W/System.err(19317):    at android.content.ContextWrapper.openFileInput(ContextWrapper.java:159)
  10-27 10:23:31.660: W/System.err(19317):    at com.sriram.hellomediaplayer.HelloMediaPlayer.onCreate(HelloMediaPlayer.java:65)
  10-27 10:23:31.660: W/System.err(19317):    at android.app.Activity.performCreate(Activity.java:4465)
  10-27 10:23:31.660: W/System.err(19317):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  10-27 10:23:31.660: W/System.err(19317):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  10-27 10:23:31.670: W/System.err(19317):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  10-27 10:23:31.670: W/System.err(19317):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
  10-27 10:23:31.670: W/System.err(19317):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  10-27 10:23:31.670: W/System.err(19317):    at android.os.Handler.dispatchMessage(Handler.java:99)
  10-27 10:23:31.670: W/System.err(19317):    at android.os.Looper.loop(Looper.java:137)
  10-27 10:23:31.670: W/System.err(19317):    at android.app.ActivityThread.main(ActivityThread.java:4424)
  10-27 10:23:31.670: W/System.err(19317):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-27 10:23:31.680: W/System.err(19317):    at java.lang.reflect.Method.invoke(Method.java:511)
  10-27 10:23:31.680: W/System.err(19317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  10-27 10:23:31.680: W/System.err(19317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  10-27 10:23:31.680: W/System.err(19317):    at dalvik.system.NativeStart.main(Native Method)
  10-27 10:23:31.680: W/System.err(19317): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
  10-27 10:23:31.690: W/System.err(19317):    at libcore.io.Posix.open(Native Method)
  10-27 10:23:31.690: W/System.err(19317):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
  10-27 10:23:31.690: W/System.err(19317):    at libcore.io.IoBridge.open(IoBridge.java:390)
  10-27 10:23:31.690: W/System.err(19317):    ... 18 more
  10-27 10:23:31.700: V/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): Data source set to: /data/data/com.sriram.hellomediaplayer/files/word_recorded.wav
  10-27 10:23:31.700: V/com.sriram.hellomediaplayer.HelloMediaPlayer@410690f0(19317): Audio stream for mp set.
  10-27 10:23:31.700: I/MediaPlayer(19317): ======&gt;prepare
  10-27 10:23:31.700: I/MediaPlayer(19317): ======&gt;prepareAsync_l
  10-27 10:23:31.720: E/MediaPlayer(19317): error (1, -2147483648)
  10-27 10:23:31.720: W/System.err(19317): java.io.IOException: Prepare failed.: status=0x1
  10-27 10:23:31.720: W/System.err(19317):    at android.media.MediaPlayer.prepare(Native Method)
  10-27 10:23:31.720: W/System.err(19317):    at com.sriram.hellomediaplayer.HelloMediaPlayer.onCreate(HelloMediaPlayer.java:113)
  10-27 10:23:31.720: W/System.err(19317):    at android.app.Activity.performCreate(Activity.java:4465)
  10-27 10:23:31.720: W/System.err(19317):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  10-27 10:23:31.720: W/System.err(19317):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  10-27 10:23:31.720: W/System.err(19317):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  10-27 10:23:31.720: W/System.err(19317):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
  10-27 10:23:31.720: W/System.err(19317):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  10-27 10:23:31.720: W/System.err(19317):    at android.os.Handler.dispatchMessage(Handler.java:99)
  10-27 10:23:31.730: W/System.err(19317):    at android.os.Looper.loop(Looper.java:137)
  10-27 10:23:31.730: W/System.err(19317):    at android.app.ActivityThread.main(ActivityThread.java:4424)
  10-27 10:23:31.730: W/System.err(19317):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-27 10:23:31.730: W/System.err(19317):    at java.lang.reflect.Method.invoke(Method.java:511)
  10-27 10:23:31.730: W/System.err(19317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  10-27 10:23:31.730: W/System.err(19317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  10-27 10:23:31.740: W/System.err(19317):    at dalvik.system.NativeStart.main(Native Method)
  10-27 10:23:31.820: D/libEGL(19317): loaded /system/lib/egl/libGLES_android.so
  10-27 10:23:31.830: D/libEGL(19317): loaded /system/lib/egl/libEGL_VIVANTE.so
  10-27 10:23:31.840: D/libEGL(19317): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
  10-27 10:23:31.860: D/libEGL(19317): loaded /system/lib/egl/libGLESv2_VIVANTE.so
  10-27 10:23:31.900: D/OpenGLRenderer(19317): Enabling debug mode 0
  </code></pre>
  
  <p>What I have tried so far:<br>
  1. Changed permissions on the file stored internally with <code>file.setReadable</code>. The command fails to set <code>WORLD_READABLE</code> permissions.<br>
  2. Tried to copy it into another file which was created specifically with <code>WORLD_READABLE and WORLD_WRITEABLE</code> permissions. Oddly, I get a <code>FileNotFound</code> error despite the previous lines saying that the file <code>word_recorded.wav</code> was present.<br>
  3. The permissions on word_recorded.wav suggest that the file is readable but the program indicates otherwise.<br>
  4. Pulled the wav file out and played it on my system. It plays just fine.<br>
  5. Passed a <code>FileDescriptor</code> to the program. This has not worked either.  </p>
  
  <p>My question(s):<br>
  1. Where am I going wrong? How do I make this work?</p>