Dev pro
Dev pro

Reputation: 630

Can't open an existing file in Sdcard in Android Studio

I have this code in my project to read an audio file :

MainActivity2:

private String getFilename() { 
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);
    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}

private void startRecording() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());
    Log.i("Tag", getFilename());
    ListeAudio.put(indice,getFilename()); // here is ListeAudio
    path=getFilename();
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);
    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

MainActivity:

File AudioFile = new File( MainActivity2Activity.ListeAudio.get(z));
    // ListeAudio return /storage/sdcard0/AudioRecorder/1433288052123.mp4    
    Uri AudioUri = Uri.fromFile(AudioFile);
    vocal = MediaPlayer.create(MainActivity.this,AudioUri);
    vocal.start();

that's doesn't work and in logcat there is an error who say: Failed to open file '/storage/sdcard0/AudioRecorder/1433288052123.mp4'. (No such file or directory) And when i search manually this path in the file explorer i found the audio file wich i want to play. I have this in the AndroidManifest:

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

Please help.

Upvotes: 0

Views: 1627

Answers (2)

alijandro
alijandro

Reputation: 12147

What the implementation of your call MainActivity2Activity.ListeAudio.get(z)? Try to use the system api to access the external storage.

if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {
    File audioFile = new File(Environment.getExternalStorageDirectory()
                .getAbsoluteFile()
                + File.separator
                + "AudioRecorder/1433288052123.mp4");
    if (audioFile.exists()) {
        MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.fromFile(audioFile));
        mediaPlayer.start();
    }
}

// EDIT

    recorder.setOutputFile(getFilename());
    Log.i("Tag", getFilename());
    ListeAudio.put(indice,getFilename()); // here is ListeAudio

The file path your write to and the file path your save are not the same file, getFilename() will return a unique file path every time it get called.

Change to this, if you want to use current milliseconds as the file name.

    final String filePath = getFilename();
    recorder.setOutputFile(filePath);
    Log.i("Tag", filePath);
    ListeAudio.put(indice, filePath); // here is ListeAudio

Upvotes: 1

Grux
Grux

Reputation: 584

You can use getExternalFilesDir() instead of hard-coding your filepath.

File AudioFile = new File(context.getExternalFilesDir() + "/" + "yourFile.ext")

Upvotes: 0

Related Questions