Android accessing files by uri results with SecurityExcepption

101 Views Asked by At

I´m stuck with an security exception. I save audio file in my app and can play it. It works fine until device reboot. After reboot the app the trouble begins.

What I try to do is just start a media player which is created from URI. Here is how I do it: First I create ForegroundService which is created like:

Intent sceneService = new Intent(context, SceneManager.class);
sceneService.putExtra("file", mFile);
context.startForegroundService(sceneService);

Then the service starts the media player class to start the audio file

public class SceneManager extends Service
{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
       GameAudioPlayer player = GameAudioPlayer.getInstance();
       player.startScene(this, audio)
    }
}

The GameAudioPlayer does stuff:

MediaPlayer player = MediaPlayer.create(context, Uri.parse(mFile.getUri()));
player.start()

And the result is:

W/MediaPlayer: Couldn't open content://com.android.externalstorage.documents/document/primary:Music/my/path/to/file.mp3
    java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{2b6811f 18501:com.wintermute.adventuresmaster/u0a351} (pid=18501, uid=10351) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
        at android.os.Parcel.createException(Parcel.java:2071)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.os.Parcel.readException(Parcel.java:1987)
        at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:5156)
        at android.app.ActivityThread.acquireProvider(ActivityThread.java:6807)
        at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2762)
        at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:2163)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1689)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1521)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1438)
        at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1127)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1099)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1011)
        at android.media.MediaPlayer.create(MediaPlayer.java:918)
        at android.media.MediaPlayer.create(MediaPlayer.java:895)
        at android.media.MediaPlayer.create(MediaPlayer.java:874)
        at com.wintermute.adventuresmaster.services.player.GameAudioPlayer.preparePlayer(GameAudioPlayer.java:115)
        at com.wintermute.adventuresmaster.services.player.GameAudioPlayer.lambda$playScene$1$GameAudioPlayer(GameAudioPlayer.java:99)
        at com.wintermute.adventuresmaster.services.player.-$$Lambda$GameAudioPlayer$nydFlOv8edPL3wGxNrMmFC7swLI.accept(Unknown Source:6)
        at java.util.ArrayList.forEach(ArrayList.java:1262)
        at com.wintermute.adventuresmaster.services.player.GameAudioPlayer.playScene(GameAudioPlayer.java:99)
        at com.wintermute.adventuresmaster.services.player.SceneManager.onStartCommand(SceneManager.java:84)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4204)
        at android.app.ActivityThread.access$2100(ActivityThread.java:231)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1984)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:7700)
        at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:8028)
        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2127)
        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3015)
        at android.os.Binder.execTransactInternal(Binder.java:1032)
E/MediaPlayerNative: Unable to create media player
0

There are 0 best solutions below