Android Question Build path to App B from App A

luke2012

Well-Known Member
Licensed User
Hi all,
I need to access a file within App B from App A and in this case the file is a SQLite db.

Usually I can access the db files within an app using:

B4X:
    SQL1.Initialize(File.DirDefaultExternal, "db1.db" , False)

How to access a db file within App B from App A ?

B4X:
    'Code within App A
    private PATH_TO_DB_FILE_WITHIN_APP_B as string = "?"
    SQL2.Initialize(PATH_TO_DB_FILE_WITHIN_APP_B, "db2.db" , False)

Is it possible to build the <path_to_db_file_within_app_b> ?

P.S.
App A and App B are installed within the same device and I known the pkg name of both Apps.

Thanks in advance for your reply :)
 

luke2012

Well-Known Member
Licensed User
Yes. You can find it with Log(File.DirDefaultExternal).

And I can use this path (File.DirDefaultExternal of App B) within App A code to open the db file within the App B path ?

I mean no issues about Android data access security ?
 
Upvote 0

DonManfred

Expert
Licensed User
And I can use this path (File.DirDefaultExternal of App B) within App A code to open the db file within the App B path ?
App A does not have the permission to access the path from App B i would think... so i guess the answer is NO.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
I did a test from App A I try to initialize a db within App B (both app are developed by me and are signed with my key) but I got the following error:

B4X:
Logger connected to:  LGE Nexus 4
--------- beginning of main
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 30 (Main)
java.io.FileNotFoundException: /storage/emulated/0/Android/data/b4a.example/files/virtual_assets/listini.db: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:456)
    at java.io.FileInputStream.<init>(FileInputStream.java:76)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:199)
    at anywheresoftware.b4a.objects.streams.File.Copy(File.java:335)
    at luke2012.android.lclibrary.classfsmanager._copyfromassetstoapp(classfsmanager.java:45)
    at luke2012.android.lclibrary.classsqlitemanager._initialize(classsqlitemanager.java:595)
    at b4a.example.main._activity_create(main.java:378)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at b4a.example.main.afterFirstLayout(main.java:102)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:442)
    ... 25 more
** Activity (main) Resume **

I copied the file path that will be retuned from Log(File.DirDefaultExternal) running App B and I use it to initialize the db file within the App A.
If I understood correctly, seem that the App A cannot "see" the DirDefaultExternal path of App B.

My target is "to tell" to the App B to update a field value (within the App B sqlite db) when a specific event occur in App A.
 
Upvote 0

LucaMs

Expert
Licensed User
As far as I know, DirDefaultExternal belongs to app. Surely this would work if you use the DirRootExternal.
 
Upvote 0

LucaMs

Expert
Licensed User
You can access any folder under File.DirRootExternal including File.DirDefaultExternal of a different app.
So, one more reason to use DirRootExternal directly (in this case), since also DirDefaultExternal is not protected.
 
Upvote 0

PCastagnetti

Member
Licensed User
I attach the code where the application 'a' passes as parameter the path to the application 'b' and opens the db:

Application 'a' calls application 'b'
B4X:
Dim in As Intent
in = pm.GetApplicationIntent("test.test")'--> insert the 'b' package name
in.PutExtra("CallerPath",File.DirDefaultExternal)
StartActivity(in)

Application 'b' in activity_create:
B4X:
Dim In As Intent
    In = Activity.GetStartingIntent
    If In <> Null Then
        Dim TmpPath As String = In.GetExtra("CallerPath")
        If File.Exists(TmpPath,"MyDB.db") Then
            sql.Initialize(TmpPath, "MyDB.db", True)
        End If
  
    End If
This code works for me
 
Last edited:
Upvote 0
Top