Android Question [SOLVED] Launching a TextEditor with a parameter on startup

T201016

Active Member
Licensed User
Longtime User
Hello everyone,
here's how to run your own application (text editor) with the selected file.

This example allows you to open the contents of a text file (.txt) and an encrypted file (.weo) in mine
in the previous example, TextEditor.

However, I would rather research the file type in terms of its contents than just by the file extension,
but so far I have not found any other solution:


in the Manifest:
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="18" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

'FileProvider
AddApplicationText(
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$PACKAGE$.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
)
CreateResource(xml, provider_paths,
<files-path name="name" path="shared" />
)
'IME
SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)

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=".*\\.txt" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.txt" />
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.txt" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.txt" />
<data android:scheme="http" android:host="*" android:pathPattern=".*\\.weo" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.weo" />
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.weo" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.weo" />
<data android:mimeType="*/*" />
</intent-filter>)

In the code:
B4X:

In the code::
    Sub Activity_Create(FirstTime As Boolean)

        Private In As Intent = Activity.GetStartingIntent
        Private FileNameParameter As String = In.GetData

        If FileNameParameter <> Null Then
            MsgboxAsync(FileNameParameter,"Parameters")
            ... 'Open text editor with parameter.
            If FileName <> *.txt or *.weo Then MsgboxAsync("The wrong file type has been select.", "Correct file: *.txt | *.weo")
            ExitApplication
        Else
            ... 'Open text editor without parameters.
        End If
    End Sub
 

T201016

Active Member
Licensed User
Longtime User
I wonder if there is a quick way besides (MIME) examining files with different extensions for whether they are a text file.

Something like:


isClassFile::
Sub IsClassFile (Path As String, FileName As String) As Boolean
     If Not (File.Exists (Path, FileName)) OR File.IsDirectory (Path, FileName) Then Return False
     If File.Size (Path, FileName) <4 Then Return False
     Dim RAF As RandomAccessFile
     RAF.Initialize2 (Path, FileName, True, False)
     Dim result As Boolean = RAF.ReadInt (0) = 0xCAFEBABE
     RAF.Close
     Return result
End Sub
 
Upvote 0
Top