Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set file permission to MODE_WORLD_READABLE

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
like image 600
Ted Murphy Avatar asked Jun 25 '12 12:06

Ted Murphy


2 Answers

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);
like image 122
Idolon Avatar answered Nov 15 '22 07:11

Idolon


  • 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();
like image 44
user370305 Avatar answered Nov 15 '22 06:11

user370305