1

I have an app that downloads a specified sound file from the internet and saves it to the device. That is working fine as I am able to see the file in ddms. I also pulled the file from the device to verify it wasn't corrupt and it is fine. The problem is when I try to play that file I get permission denied.

I created the file using:

FileOutputStream('path/to/file') 

and like I said it created the file and saved it just fine so I'm not going to include the full code on that. But when I try to run it using:

File file = new File(getFilesDir() + File.separator + "sounds", fileName);
MediaPlayer mediaPlayer = MediaPlayer.create(context, Uri.fromFile(file));
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
    }
});
mediaPlayer.start();

is when I get the permission error:

Failed to open file '/data/data/com.myapp/files/sounds/sound_2.mp3'. (Permission denied)

Is there something I am supposed to do when I create the file? It appears to have read and write permissions when I view the file in ddms but it just won't let me open it.

I also have this permission in manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

Edit: After trying various things on the web I tried:

openFileOutput('file_name.mp3', Context.MODE_PRIVATE)

which had the same result. But then I tried:

openFileOutput('file_name.mp3', Context.MODE_WORLD_READABLE)

and that worked!

So I compared the file permissions. Using the first way (FileOutputStream) it created the file with the permissions -rw-----, with openFileOutput using the flag MODE_PRIVATE it had the permissions -rw-rw---- and with MODE_WORLD_READABLE it had the permissions -rw-rw-r--

MODE_WORLD_READABLE is deprecated and the documentation says not to use it because it is dangerous. So is there some other way to be able to open files created by the same app?

Edit2 I have pretty much determined it is something to do with the way MediaPlayer works. It appears to open the file like it is not in the same app. So it gets the permission errors unless it is world readable. I don't know if this is a bug or intended but if anyone has a workaround I would be greatful.

Brian
  • 903
  • 1
  • 7
  • 13

0 Answers0