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!
 

Mahares

Expert
Licensed User
Longtime User
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.
You need to show the code for writing and reading the file using runtimepermissions. Also what may help is your targetSDK in manifest and your device OS. Based on what you supplied, it is not sufficient
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Target SDK in Manifest is 28, the os is 8.1. The code to write is irrelevant because I dont know where to write to the file as the target app obviously can look up but not read in the file structure of my b4a app. I may have made not clear what my question is, but I would like to know if the Android system provides a place where files can be exchanged (DCIM or similar) without mutual permissions (I cannot change the target app) or alternatively how I could write a file into the target apps file structure (which is known as DroidFish/epd) . It obviously is possible on an external sd card, but my phone has no slot for such a card.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I could write a file into the target apps file structure (which is known as DroidFish/epd)
B4X:
    ' DON'T TARGET ABOVE API 28 FOR THE TIME BEING AS WE NEED TO WORK OUT STORAGE ACCESS
    ' Need to get all the first time things done before Wait For as FirstTime is no longer set on re-entry after a wait
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)   
    Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)
See the entries for API 29 and 30
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Thank you all. Give me some time to go through your suggestions and to read the tutorials.
Erel, the chess app has a menu to pick a file to import anywhere on the device. That's where I can see the file I produced when dropped for instance into the DirInternal of my own app. But it fails to read the choosen file as long it is not in the chess app's own file system. So it definetly has to do something with permissions. I have worked with many different operating systems but the file permissions of Android are mystery to me. But I advanced one step further: If I try to drop the file into the DroidFish folder for that purpose, I get a "permission denied" error. That is certainly a good starting point for a solution.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
No, you cannot choose an app as source of the text file. You can choose a location where to pick up a file. There's a default location where it would look for a file which is DroidFish/epd but I have no rights to place the file there. The DroidFish would allow me to choose a file in any other directory with a sort of file explorer, but as said before, it does not read it. I have a link to the manual, on page 11 you see the relevant info. https://raw.githubusercontent.com/peterosterlund2/droidfish/master/doc/droidfish_manual.pdf. Mine is not a commercial product so I can choose any target SDK that runs on my android 8 phones.
So my question remains: is it possible to write a file in any other place than sd card root or DirInternal ? Maybe just a little example with the relevant code would be fantastic!
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I have found a solution to solve the problem, although not in b4a. The chess program can cooperate with a file manager called OI FileManager (from playstore) and use this filemanager to pick up a file I did drop at my apps File.DirDefaultExternal. However, I would still be interested to know how to get permissions and write a file to other shared directories. The solution from Agraham is too cryptic for me - sorry- i could not integrate it into my app.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The solution from Agraham is too cryptic for me - sorry- i could not integrate it into my app.
That's hardly cryptic! Put those few lines in Activity_Create and then the Files methods will magically work to let you write where you want in external memory as long as you are targeting API 28 or earlier.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Sorry Agraham, what's clear for an expert like you may be cryptic for a dummy. Now that you mention it should be copied into the activity create, thats already clearer. And it probably needs a handler for the "wait for" event, isnt it? I will try again. And it's always worth to watch Erels tutorials. Again thank you all.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Check this application:


How does it do it to read and write in other apps' file structure? As far as I know my device is not rooted.
 
Last edited:
Upvote 0

MikeSW17

Active Member
Licensed User
Might be of help. While making some screenshots of the DroidFish I noticed the DroidFish settings screens were saved with filenames ending"_DroidFish".
But shots of the 'file manager' end with "_Total-Commander". I believe this is a separate file manager app?
Anyhow, to help others understand the issue being discussed, here are some screen-shots.
Screenshot_20201102-174107_DroidFish.jpg Screenshot_20201102-174123_Total Commander.jpg Screenshot_20201102-174140_Total Commander.jpg Screenshot_20201102-174200_DroidFish.jpg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Again my excuses, Agraham, I am very much willing to try but I have got some other things to look after that do not have anything to do with progamming but with cooking, walking my dog and looking after my spouse who is not well after an operation.

So, here we are: Your code snipped has been included at the end of the create_activity sub. The log file after the call "wait for" returns "false" which is obviously not permitting write access.

B4X:
    Dim rp As RuntimePermissions
    Dim Result As Boolean
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Log("PERMISSION_WRITE_EXTERNAL_STORAGE: "&Result)
I have not yet been able to listen to Erels tutorial, which I will do tomorrow, maybe I have to add other things for instance in the mainifest file which looks like this:
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: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

Please have some patience with old guys! ;)
 
Last edited:
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Yes indeed, uninstall did it. When I restarted my app, the pop up came up and asked me for the permission to write, I acknowleded, and I got a true as response. I will write files tomorrow, but thank you ever so much for your good advice and patience!
 
Upvote 0
Top