Android Question How can I open a file from File.DirInternal?

PhrotonX

Member
In the code below, I could not be able to use FileProvider because simply adding that library into my project causes my app to crash on an Android 15 emulator and my physical Android 10 device.

Also, I have no idea on how could I open some files located within a subfolder (in this case, m_fileRepository.DIRECTORY is "/attachments/" of type String) without using RuntimePermissions or FileProvider because both libraries make my app crash by simply enabling them.

I am currently on B4A version 13.10 64-bit and Android SDK 34.

B4X:
Public Sub OpenAttachment(attachment_id As Long)
    Dim filePath As String = File.Combine(File.DirInternal & m_fileRepository.DIRECTORY, GetAttachment(attachment_id).GetFilename)
   
    Log(filePath)
   
    Dim intentObj As Intent
    intentObj.Initialize(intentObj.ACTION_VIEW, "file://" & filePath) ' Does not work.
    intentObj.SetComponent("android/com.android.internal.app.ResolverActivity")
    intentObj.SetType("*/*")
    StartActivity(intentObj)
End Sub

B4X:
/data/user/0/com.cm.todolist/files/attachments/1744435111898_IMG_20250407_204849.jpg
Error occurred on line: 50 (AttachmentViewModel)
android.os.FileUriExposedException: file:///data/user/0/com.cm.todolist/files/attachments/1744435111898_IMG_20250407_204849.jpg exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:2265)
    at android.net.Uri.checkFileUriExposed(Uri.java:2402)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:12524)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:12472)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1999)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1161)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1132)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:438)
    at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:861)
    at com.cm.todolist.attachmentviewmodel._openattachment(attachmentviewmodel.java:138)
    at com.cm.todolist.editoractivity._clvattachments_itemclick(editoractivity.java:1333)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
    at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1077)
    at b4a.example3.customlistview$ResumableSub_PanelClickHandler.resume(customlistview.java:805)
    at b4a.example3.customlistview._panelclickhandler(customlistview.java:748)
    at b4a.example3.customlistview._panel_click(customlistview.java:735)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:8028)
    at android.view.View.performClickInternal(View.java:8005)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:31229)
    at android.os.Handler.handleCallback(Handler.java:959)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loopOnce(Looper.java:232)
    at android.os.Looper.loop(Looper.java:317)
    at android.app.ActivityThread.main(ActivityThread.java:8705)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
** Activity (editoractivity) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**

My manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="24"
android:targetSdkVersion="34" />
<supports-screens android:largeScreens="true"
    android:debuggable="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)
AddManifestText(
<uses-permission
  android:name="android.permission.READ_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)
AddManifestText(
<uses-permission
  android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)

CreateResource(xml, provider_paths,
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="root" path="" />
</paths>
)
#AdditionalJar: com.android.support:support-core-utils
'End of default text.


My Libraries:
1744453632940.png


The error log from the ADB logcat if I enabled FileProvider library and opened the application:

B4X:
04-12 18:33:39.903 24587 24587 D AndroidRuntime: Shutting down VM
04-12 18:33:39.908 24587 24587 E AndroidRuntime: FATAL EXCEPTION: main
04-12 18:33:39.908 24587 24587 E AndroidRuntime: Process: com.cm.todolist, PID: 24587
04-12 18:33:39.908 24587 24587 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cm.todolist/com.cm.todolist.main}: java.lang.ClassNotFoundException: Didn't find class "com.cm.todolist.main" on path: DexPathList[[zip file "/data/app/~~n2cpsmOJ-ztPi0L3hFWv4Q==/com.cm.todolist-SraYYX9G8xE4wThrtu41Cw==/base.apk"],nativeLibraryDirectories=[/data/app/~~n2cpsmOJ-ztPi0L3hFWv4Q==/com.cm.todolist-SraYYX9G8xE4wThrtu41Cw==/lib/x86_64, /system/lib64, /system_ext/lib64]]
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3954)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4235)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:112)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:174)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:109)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:81)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2636)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:107)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:232)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:317)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8705)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
04-12 18:33:39.908 24587 24587 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.cm.todolist.main" on path: DexPathList[[zip file "/data/app/~~n2cpsmOJ-ztPi0L3hFWv4Q==/com.cm.todolist-SraYYX9G8xE4wThrtu41Cw==/base.apk"],nativeLibraryDirectories=[/data/app/~~n2cpsmOJ-ztPi0L3hFWv4Q==/com.cm.todolist-SraYYX9G8xE4wThrtu41Cw==/lib/x86_64, /system/lib64, /system_ext/lib64]]
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:637)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.Instrumentation.newActivity(Instrumentation.java:1448)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3941)
04-12 18:33:39.908 24587 24587 E AndroidRuntime:        ... 13 more
 
Last edited:

PhrotonX

Member
You must use FileProvider. Test it on a real device. What happens when you run this example: https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/ ?
That example crashes as well, both on my Android 15 emulator and my physical Android 10 device.

B4X:
04-13 10:21:13.092  4346  4346 D AndroidRuntime: Shutting down VM
04-13 10:21:13.093  4346  4346 E AndroidRuntime: FATAL EXCEPTION: main
04-13 10:21:13.093  4346  4346 E AndroidRuntime: Process: b4a.example, PID: 4346
04-13 10:21:13.093  4346  4346 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{b4a.example/b4a.example.main}: java.lang.ClassNotFoundException: Didn't find class "b4a.example.main" on path: DexPathList[[zip file "/data/app/~~TiCyervJyXWjmmWf3YbwZw==/b4a.example-1s4TJkj2ND-UAmlMJpg2vA==/base.apk"],nativeLibraryDirectories=[/data/app/~~TiCyervJyXWjmmWf3YbwZw==/b4a.example-1s4TJkj2ND-UAmlMJpg2vA==/lib/x86_64, /system/lib64, /system_ext/lib64]]
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3954)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4235)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:112)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:174)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:109)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:81)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2636)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:107)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:232)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:317)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8705)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
04-13 10:21:13.093  4346  4346 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "b4a.example.main" on path: DexPathList[[zip file "/data/app/~~TiCyervJyXWjmmWf3YbwZw==/b4a.example-1s4TJkj2ND-UAmlMJpg2vA==/base.apk"],nativeLibraryDirectories=[/data/app/~~TiCyervJyXWjmmWf3YbwZw==/b4a.example-1s4TJkj2ND-UAmlMJpg2vA==/lib/x86_64, /system/lib64, /system_ext/lib64]]
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:637)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.Instrumentation.newActivity(Instrumentation.java:1448)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3941)
04-13 10:21:13.093  4346  4346 E AndroidRuntime:        ... 13 more


I already stated above that my application also crashes on my physical Android 10 device.
 
Upvote 0

PhrotonX

Member
It looks like your device fails to run the app once it switches to "multidex" mode. Is this a regular Android 10 device?

Can you post a screenshot of Tools - Configure Paths?
I tried it both on my Android 15 emulator and my regular/stock Android 10 device. Both failed.

1744524872875.png
 
Upvote 0

PhrotonX

Member
This means that there is a problem on your building machine. I don't think that this is the cause of this issue however the Additional libraries folder shouldn't be under Program Files.

Maybe your antivirus breaks the compilation process.
I tried moving the Additional Libraries folder elsewhere (and removed its whitespace), and then disabled my antivirus, but it didn't work.

I also tried adding #MultiDex: True but it didn't work.

B4X:
#Region  Project Attributes
    #ApplicationLabel: To Do List
    #VersionCode: 10
    #VersionName: v2.0.0.2 alpha
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #MultiDex: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

1744706605181.png
 
Upvote 0

PhrotonX

Member
I have already fixed this problem after using Android SDK 33, although my build tools remain in version 34.

Now, I have already used GetSafeDirDefaultExternal instead of DirInternal but I am now encountering with several problems regarding file paths and invocationTargetException. I had to make a separate thread for it.

1744790290521.png
 
Upvote 0
Top