How do I set file permission to MODE_WORLD_READABLE

android, media-player

Solution

Call `openFileOutput()` with the name of the file and the operating mode. This returns a `FileOutputStream` with permission `MODE_WORLD_READABLE`.

Write to the file with `write()`.

Close the stream with `close()`.

For example: (Code for txt file, in your case its a Audio file make changes according to it)

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();

Problem

I am downloading an audio file to my Android application's cache that must then be accessed with MediaPlayer. The file gets created with permissions of -rw------- (as seen in Eclipse's File Explorer). With these permissions, the MediaPlayer cannot access the audio file. Failure is shown in logcat as: ``` java.io.IOException: Prepare failed.: status=0x1 ``` My app targets API level 8. How do I set the downloaded audio file's permissions to make it readable by the MediaPlayer? By the way, I am creating the file with: ``` FileOutputStream out = new FileOutputStream(fRecording); ``` When I try to create the file as MODE_WORLD_READABLE, no file is created: ``` FileOutputStream out = activity.openFileOutput(fRecording.getAbsolutePath(), Activity.MODE_WORLD_READABLE); ``` The logcat error using openFileOutput is: ``` java.lang.IllegalArgumentException: File /data/data/com.calltrunk.android/cache/727adda7-0ef1-490f-9853-fe13ad1e9416 contains a path separator ```

Original source