Android Question Read file with custom extension

Sergey_New

Well-Known Member
Licensed User
Longtime User
I read almost the entire forum, but I still couldn't find solutions on how to read a text file with the ".ged" extension, when a file is selected by the device's file manager.
I have to repeat the question again.
I found several manifest examples. For example:
B4X:
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" android:host="*" android:pathPattern=".*\\.ged" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.ged" />
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.ged" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.ged" />
<data android:mimeType="*/*" />
</intent-filter>)
None of the examples set the default value in the application settings.
Although the application is launched for the first time with the option to select "always" or "only now", it is impossible to change this choice later.
I wonder where these settings are saved?
Even if I delete the application and reinstall it, the device does not offer to make a choice.
If there is another application on the device for reading such files, then when you delete one of them, the remaining application will also read the file without the ability to change the choice.
What can be done?
And another question:
How can I find out the file name and path in the given example?
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim In As Intent = Activity.GetStartingIntent 
    Dim Parametri As String = In.GetData
    If Parametri<>Null Then
        Activity.LoadLayout("print")
        LabelInfo.Text="Print file: " & Parametri
    Else
        Activity.LoadLayout("main")
    End If
End Sub
I have attached an example.
 

Attachments

  • test_ged.zip
    3.4 KB · Views: 16
Last edited:

Sergey_New

Well-Known Member
Licensed User
Longtime User
You should be able to open it as a simple txt file.
Try selecting the ged-file with your phone's file manager. What will be the result?
My phone is a Samsung Galaxy S23 FE Exynos.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
@zed is correct - treat .ged files as you would any text file. I wrote a gedcom viewer many years ago - today I might do it differently - but here is the code that I used :

B4X:
' Read the Gedcom file in [filename]
Public Sub readFile(dir As String, filename As String)
    Dim f As InputStream
    f = File.OpenInput(dir, filename)
    reader.Initialize(f)
    currentLine = Parse(reader.ReadLine)
    Do While (currentLine.level = 0)
        Select currentLine.tag
            Case "HEAD"    :    readHeader
            Case "INDI"    :    readPerson
            Case "FAM"    :    readFamily
            Case "TRLR"    :    readTrailer
        Case Else
            skipRecord
        End Select
    Loop
    FList.SortType("key", True)    ' Sort in preparation for binary searching
End Sub

"reader" here is a TextReader. "Parse" is a subroutine that parses a gedcom item into a gedcom object. I don't understand what you are trying to do with the manifest and file managers, so maybe I have misunderstood your question.
 
Last edited:
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
so maybe I have misunderstood your question.
Yes, you misunderstood my question.
You need to select the ged-file in the phone's file manager and send it to the app for reading.
I have explained all this above.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
You need to select the ged-file in the phone's file manager and send it to the app for reading.
Yes - I understand now : you are trying to set up an intent filter to declare you app's interest in reading .ged files. Not something that I have any experience of - sorry.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
thank you @zed! at least somebody is using search. what i posted worked for me then and still works.
 

Attachments

  • 1.png
    1.png
    33.6 KB · Views: 23
  • 2.png
    2.png
    24 KB · Views: 24
  • 3.png
    3.png
    27.5 KB · Views: 22
  • Like
Reactions: zed
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
watch this Post26
If you look closely, you will see that the thread at the specified link was created by me last year. Unfortunately, the issue was not resolved, so after many unsuccessful attempts, I created this thread.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
at least somebody is using search. what i posted worked for me then and still works.
Of course, I appreciate your answer in the previous thread. But as I wrote, it didn't work for me. Could you attach a small example to make sure that it might be related to my code.
 
Upvote 0

geps

Member
Licensed User
Longtime User
The settings are stored in the system's shared preferences or in the app's internal data, so just reinstalling the app might not reset them. To fix the "default choice" problem, you might need to reset the app’s default handler for that file type in the device’s settings. Sometimes clearing the defaults for the specific file type in the Android settings (under Apps > Your App > Open by Default) can help.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
To fix the "default choice" problem, you might need to reset the app’s default handler for that file type in the device’s settings.
There are no GED extensions in the device settings.
under Apps > Your App > Open by Default

The application does not have this option installed.
All of this is discussed above.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Of course, I appreciate your answer in the previous thread. But as I wrote, it didn't work for me. Could you attach a small example to make sure that it might be related to my code.
export your project and post it.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if you're going to let the user choose a file from within your app, you would use contentchooser.

if you're going to let the user chose a file from the files/downloads/documents folder, then you use an intent. BUT the intent is handled in activity_resume, not in activity_create.

i don't know what all your manifest is doing, but use the one i posted. this will allow the user to share a .ged file with your app.

if you still want the user to select a file from within your app, then i believe you will have to use the "*/*" mime type.

for the purpose of simply loading and displaying a .get file, i have used a label. it can be used whether or not you are using the contentchooser or an intent filter.

to use the contentchooser:
B4X:
dim cc as contentchooser
cc.Initialize("CC")
cc.Show("*/*", "choose ged")

wait for CC_Result(Success As Boolean, Dir As String, FileName As String)
If Success Then
    label1.Text = File.ReadString(Dir, FileName)
Else
    ToastMessageShow("failed or no file selected",False)
End If

to use an intent filter:
B4X:
Sub Activity_Resume
    Dim intent As Intent = Activity.getstartingintent
    Dim action As String = intent.Action
    Log(action)
    Dim myextras As String = intent.ExtrasToString
    Log(myextras)
    Dim uri As String = intent.GetData
    Log(uri)
    If uri <> Null Then
        label1.Text = File.ReadString("ContentDir", uri)
    End If
end sub
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
to use an intent filter:
Thank you! My example using Activity_Resume started working well. Now you can select "Always - Now Only" and cancel it.
My question remains how to get the file name (my.ged) from Log(uri):
B4X:
content://com.android.externalstorage.documents/document/primary%3ADownload%2Fmy.ged
 
Upvote 0

emexes

Expert
Licensed User
edit:
I got this wrong, thought that "my.get" was the file extension, and %2F was a full stop "."
on the bright side, is still a good example of "manually" splitting up a string using .LastIndexOf string search


Maybe something like:
B4X:
Dim TestList() As String = Array As String(                                                 _
    "content://com.android.externalstorage.documents/document/primary%3ADownload%2Fmy.ged", _
    "https://redlightcemetery.com/RedLightCemeteryQuokka%2Fjpeg",                           _
    "https://live.staticflickr.com/5490/9079273886_2006eb1731_z.jpg",                       _
    "file_name_with_no_extension"                                                           _
)

For Each uri As String In TestList
    For Each Separator As String In Array As String("%2F", ".")
        Dim P As Int = uri.ToUpperCase.LastIndexOf(Separator)
        If P >= 0 Then Exit
    Next
  
    If P < 0 Then
        Dim fname As String = uri
        Dim fextension As String = ""
    Else
        Dim fname As String = uri.SubString2(0, P)
        Dim fextension As String = uri.SubString(P + Separator.Length)
    End If

    Log("------------------------------")     
    Log("uri" & TAB & uri)
    Log("nam" & TAB & fname)
    Log("ext" & TAB & fextension)
Next
Log output:
Waiting for debugger to connect...
Program started.
------------------------------
uri    content://com.android.externalstorage.documents/document/primary%3ADownload%2Fmy.ged
nam    content://com.android.externalstorage.documents/document/primary%3ADownload
ext    my.ged
------------------------------
uri    https://redlightcemetery.com/RedLightCemeteryQuokka%2Fjpeg
nam    https://redlightcemetery.com/RedLightCemeteryQuokka
ext    jpeg
------------------------------
uri    https://live.staticflickr.com/5490/9079273886_2006eb1731_z.jpg
nam    https://live.staticflickr.com/5490/9079273886_2006eb1731_z
ext    jpg
------------------------------
uri    file_name_with_no_extension
nam    file_name_with_no_extension
ext
 
Last edited:
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Maybe something like:
Thank you!
On the emulator, the return value was
B4X:
content://com.android.externalstorage.documents/document/primary%3ADownload%2Fmy.ged
I started checking on the device to read the file from the Download folder. The uri value changed to
B4X:
content://com.alphainvertor.filemanager.fileprovider/root/storage/emulated/0/Download/my.ged
I define the file name as follows:
B4X:
Sub Activity_Resume
    Dim intent As Intent = Activity.getstartingintent
    Dim uri As String = intent.GetData
    lbl2.Text=uri
    If uri <> Null Then
        Dim n As Int=uri.LastIndexOf("/")
        Dim fn As String=uri.SubString(n+1)
        lbl.Text=fn
    End If
End Sub
Now I need to copy the received file to another folder created by the user, for example GedCom. How can I do this?
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Unfortunately, when installing the application on the device, there is no choice of default value, but on the emulator there is. Is there anything that can be done?
 
Upvote 0
Top