Android Question Open Audio on Android 7

Multiverse app

Active Member
Licensed User
Longtime User
The following code worked on pre Nougat devices:
B4X:
    Dim musicin As Intent
    musicin.Initialize(File.DirRootExternal&"/Music", "1.mp3")
    musicin.SetType("audio/*")
    StartActivity(musicin)

On Nougat, it returns the following error:
(Exception) java.lang.Exception: android.os.FileUriExposedException: file:///storage/emulated/0/Music/1.mp3 exposed beyond app through Intent.getData()

I tried
https://www.b4x.com/android/forum/threads/sharing-files-from-your-app-with-file-provider.70458/

But, No luck :(
 

Multiverse app

Active Member
Licensed User
Longtime User
I still get a java.lang.reflect.InvocationTargetException error

My code:
B4X:
Sub OpenAudio
   
    Dim i As Intent
    i.Initialize(i.ACTION_VIEW, "")
    i.SetType("audio/*")
   
    Dim Uris As List
    Uris.Initialize
        Dim u As Uri = CreateFileProviderUri(File.DirRootExternal&"/Music", "1.mp3")
        Uris.Add(u)
    Dim jo As JavaObject = i
    jo.RunMethod("putParcelableArrayListExtra", Array As Object("android.intent.extra.STREAM", Uris))
    i.Flags = 1
   
   
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f)) 'Error occurs here
End Sub

Log:
Error occurred on line: 2090 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at com.multiverse.jarvis.main._createfileprovideruri(main.java:7280)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
at com.multiverse.jarvis.main._mainfunctions(main.java:2810)
at com.multiverse.jarvis.main._lblsugg_click(main.java:11725)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11180)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Music/iyaz replay.mp3
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
... 32 more
(Exception) java.lang.Exception: java.lang.reflect.InvocationTargetException
 
Upvote 0
Top