ExoPlayer не может найти файл

На устройстве есть файлы с названиями myaudio1622886261.mp3 и т.д. Пытаюсь реализовать воспроизведение их через ExoPlayer. Передаю в активити путь к файлу и проигрываю следующим образом:

private void songPlay(String songPath) {

    Uri uri = Uri.parse(songPath);

    SimpleExoPlayer simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();

    DataSource.Factory factory = new FileDataSource.Factory();

    DefaultTrackSelector trackSelector = new DefaultTrackSelector(this);

    simpleExoPlayer = new SimpleExoPlayer.Builder(this).setTrackSelector(trackSelector).build();

    MediaSource mediaSource = new ProgressiveMediaSource.Factory(factory).createMediaSource(uri);
    simpleExoPlayer.prepare(mediaSource);

    simpleExoPlayer.setPlayWhenReady(true);
}

Сам файл лежит по такому пути: /storage/emulated/0/Music/myaudio/myaudio1622886261.mp3

Но при попытке воспроизвести выдает ошибку, хотя разрешение к хранилищу есть:

com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /storage/emulated/0/Music/myaudio/myaudio1622886261.mp3: open failed: EACCES (Permission denied)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:97)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:961)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /storage/emulated/0/Music/myaudio/myaudio1622886261.mp3: open failed: EACCES (Permission denied)
at com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:119)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:88)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83) 
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:961) 
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:919) 
Caused by: java.io.FileNotFoundException: /storage/emulated/0/Music/myaudio/myaudio1622886261.mp3: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:152)
at com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:108)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:88) 
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83) 
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:961) 
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:919) 
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)

Ответы (1 шт):

Автор решения: Tiarait
failed: EACCES (Permission denied)

говорит о том у не предоставлен доступ к файлу

во первых вам нужно в манифесте предоставить доступ

READ_EXTERNAL_STORAGE - для чтения и WRITE_EXTERNAL_STORAGE - для записи (если нужно)

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

так же для андоид 6 и выше вам нужно вручную разрешать доступ - пример

String[] perms = {"android.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE"};

int permsRequestCode = 200;

requestPermissions(perms, permsRequestCode);

так же вы можете проверить есть ли этот файл на самом деле

File file = new File(uri.getPath());
if(file.exists()) //TODO дальнейшие действия

В Android Q появилась новая функция Google : фильтр для внешнего хранилища. Быстрое решение этой проблемы - добавить этот код в файл AndroidManifest.xml:

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>
→ Ссылка