Android Question File association

MartinR

Member
Licensed User
I have developed an app (Cirel) that can open a file chosen by a user, interpret the contents, and display the information graphically. The files are binary files and I give them the extension .crl. By using the Command$ function I have coded the VB6 version so that it will run automatically and open the file when a user clicks on a Cirel data file. How do I do this in the Android environment? The files will probably arrive at the user's device by email.

Is there an equivalent of the Command$ function in B4A?

Step-by-step instructions, please!

Many thanks.

Martin
 

MartinR

Member
Licensed User
Thanks for the suggestions. I have managed to get things to work so that if I run a File Explorer app and I click on one of my .crl data files my app runs.

The code I have added to the default Manifest is:

B4X:
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern=".*.crl" />
<data android:mimeType="*/*" />
</intent-filter>)

(By the way, the examples I found suggested the pathPattern should be ".*\\.crl" but I don't understand why the double escape (\\) is required; the period (.) character doesn't need escaping does it to make it a literal? The manifest given above seems to work fine for me.)

What I have failed to achieve is the passing of the clicked file to my app. How do I get the File Explorer app, or the Android OS to send the file URI to my app? In an attempt to check if any info is being sent to my app I have added the following to my Activity_Resume sub:

B4X:
Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    Log (in.ExtrasToString)
End Sub

All I get is "no extras" in my logs. Tested in both Debug and Release modes. B4A 7.01

Thanks,

Martin
 
Upvote 0

MartinR

Member
Licensed User
I get:
(Intent) Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=b4a.CirelViewAndroid cmp=b4a.CirelViewAndroid/.main }

Martin
 
Upvote 0

MartinR

Member
Licensed User
Ah,

The previous information was incorrect: that was the result simply of running my app. When I click a file in File Explorer to run my app I get

(Intent) Intent { act=android.intent.action.VIEW dat=file:///sdcard/Cirel/cambridge12.crl typ=application/x-pkcs7-crl flg=0x3 cmp=b4a.CirelViewAndroid/.main }

and

file:///sdcard/Cirel/cambridge12.crl

as the output of log (in) and log (in.getdata) respectively.

I think we can now make progress.

Many thanks

Martin
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Make sure that the external storage permission was added (Log - List Permissions).
2. You must verify that the intent is new: https://www.b4x.com/android/forum/threads/receiving-shared-images-from-other-apps.81041/#content
3. You can access this file with:
B4X:
Dim f As String = In.GetData
Dim Dir, FileName As String
If f.StartsWith("file://") Then
 FileName = f.Substring("file://".Length)
 Dir = ""
Else If f.StartsWith("content://") Then
 FileName = f.Substring("content://".Length)
 Dir = "ContentDir"
End If
Log(File.ReadString(Dir, FileName))
 
Upvote 0

MartinR

Member
Licensed User
Erel,

I was just wondering how to split the data from the intent into an appropriate directory string and a filename string, and you have provided the answer without me even asking!

In what situation would the data from the intent start with "content://"?

Many thanks,

Martin
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In what situation would the data from the intent start with "content://"?
if you use - for example - the ContentResolver to pick a file
 
Upvote 0
Top