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
Starting with API 9 File
object has setReadable
method (also setWritable
and setExecutable
). It's two args version can be used to set the read permission for all users as well (i.e. it becomes -rw-r--r--
in your case):
File file = new File("path_to_your_file");
file.setReadable(true, false);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With