Michele
Michele

Reputation: 326

Failed to open file (Permission denied)

I'm trying to record audio via MediaRecorder class in Android, save it in a file and then play it with a MediaPlayer.

Here is where I choose the file where I want to save my audio file:

currentDate = Calendar.getInstance().getTime();
condensedDate = currentDate.toString().replaceAll("\\s", "")
fileName = getExternalCacheDir().getAbsolutePath();
fileName += File.separator + condensedDate + ".3gp";

and here I set it as the output file for MediaRecorder

Recorder.setOutputFile(fileName);

Then, in another Activity I use MediaPlayer to play that file audio:

MediaPlayer player = new MediaPlayer();
try {
     player.setDataSource(fileName);
     player.prepare();
     player.start();
} catch (IOException e) {
        e.printStackTrace();
}

This is where problems begin, nothing is played and this is my Log:

09-09 14:31:38.887 1522-26066/? E/FileSource: Failed to open file '/storage/emulated/0/Android/data/com.mycompany.dbmeter.pro/cache/SunSep0914:31:19GMT+00:002018.3gp'. (Permission denied)
09-09 14:31:38.887 1522-26066/? E/GenericSource: Failed to create data source!
09-09 14:31:38.887 25996-26064/com.mycompany.dbmeter.pro E/MediaPlayerNative: error (1, -2147483648)

This is strange because I followed step by step the official sample code that can be found here.

I tried making file world readable as suggested in this stack overflow post.

I also tried what was suggested here but nothing has changed.

What am I doing wrong?

Upvotes: 7

Views: 3770

Answers (2)

Michele
Michele

Reputation: 326

I solved the problem, for some reason the name of the file I was using caused that error.

I used to get a date, convert it to a string and eliminate all spaces; something like this:

SunSep0914:31:19GMT+00:002018.3gp

Now, while debugging my app I changed it to test.3gp and it works like a charm...

Upvotes: 5

Sambit Mallick
Sambit Mallick

Reputation: 155

Did you add permissions in your Manifest file

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

Source: https://developer.android.com/guide/topics/media/mediarecorder

Upvotes: 0

Related Questions