Android Question Passing a file to another app

gezueb

Active Member
Licensed User
Longtime User
Hi,
I programmed a b4a app that produces a text file (FEN string). This file should be read by another application not written by me (DroidFish chess engine). If I drop the file in the directory advised by the RunTimePermission.getSafeDirDefaultExternal, DroidFish allows me to pick this file, but the subsequent read fails with a no such file or directory exception, probably not having access rights.
When I copy the file into the DroidFish own directory with a file explorer, the read is okay. But I cannot access this directory from b4a. Where can I drop a file in Android so that both apps have access without an external sd card (which cannot be mounted in my phone)?
Thanks for help!
 

gezueb

Active Member
Licensed User
Longtime User
Thats weird: If my code contains:
B4X:
    File.WriteString(File.DirDefaultExternal,"FENString.txt",FENString2)
the permission in the create_activity works and I am asked if I do permit.
But if the code contains:
B4X:
    File.WriteString("/storage/emulated/0/DroidFish/epd","FENString.txt",FENString2) 'permission denied
I will not get any permission even after uninstalling the app and the File.WriteString throws an exception (of course).
So it has to do something that happens at compile time because these write statements are not yet executed when the permission is asked for.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
That is expected behaviour. You should always reference paths relative to one of the File DirXXX constants at least once in the code or B4A won't add the right manifest entry. If you add both lines the second will probably work as well.

EDIT: You shouldn't use absolute pathnames as they may vary on different devices. That's why the File DirXXX constants are there.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Again thank you for your advice. I appreciate what you say, but when you have to write into a directory of another app that is not made with b4a, how do you want to use File DirXXX?
But I will try to "cheat" B4a with a file reference in a part of code that is not executed and see what happens then.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
This works indeed: I put
If 0 > 1 Then File.WriteString(File.DirDefaultExternal,"FENString.txt",FENString2)
into the code and now the permission works and the file is placed into the mentioned direct pat of the other app.
And you say that's not cryptic, my friend?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
And you say that's not cryptic, my friend?
Watch the video tutorial and everything will be clear.

Click on Logs - List Permissions. If you don't see the permission listed there then it will never be granted at runtime. If the compiler didn't add the permission for you then you should add it yourself with AddPermission in the manifest editor.
 
Upvote 0
Top