Android Question Build path to App B from App A

Discussion in 'Android Questions' started by luke2012, Jun 28, 2017.

  1. luke2012

    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:

    Code:
    SQL1.Initialize(File.DirDefaultExternal, "db1.db" , False)
    How to access a db file within App B from App A ?

    Code:
    '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 :)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Yes. You can find it with Log(File.DirDefaultExternal).
     
    luke2012 likes this.
  3. luke2012

    luke2012 Well-Known Member 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 ?

    I mean no issues about Android data access security ?
     
  4. DonManfred

    DonManfred Expert Licensed User

    App A does not have the permission to access the path from App B i would think... so i guess the answer is NO.
     
    luke2012 likes this.
  5. luke2012

    luke2012 Well-Known Member Licensed User

    Do you agree with @DonManfred @Erel ?
     
  6. ST500

    ST500 Member Licensed User

    That's a very interesting question. I would say, try it out.
     
  7. luke2012

    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:

    Code:
    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.
     
  8. ST500

    ST500 Member Licensed User

    Can you initialize the other DB inner the starter of App A? Or does it work for one DB only?
     
  9. LucaMs

    LucaMs Expert Licensed User

    As far as I know, DirDefaultExternal belongs to app. Surely this would work if you use the DirRootExternal.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You can access any folder under File.DirRootExternal including File.DirDefaultExternal of a different app.
     
    DonManfred likes this.
  11. LucaMs

    LucaMs Expert Licensed User

    So, one more reason to use DirRootExternal directly (in this case), since also DirDefaultExternal is not protected.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    There are advantages for using File.DirDefaultExternal (two three that I can think of). If you like to discuss it then please start a new thread.
     
  13. PCastagnetti

    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'
    Code:
    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:
    Code:
    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: Jun 29, 2017
  14. ST500

    ST500 Member Licensed User

    Does it open the db only, or does the app b start too?
     
  15. DonManfred

    DonManfred Expert Licensed User

    App B is started and get the path to the db file from App A so App B can open it.
     
  16. ST500

    ST500 Member Licensed User

    OK, thx DonManfred
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice