App/DataFile Association

Dave Fitzpatrick

Member
Licensed User
Longtime User
Coming from the PC world, I familiar with using paramStr(0 and 1) to launch an application and load/show the info contained in a data file that is downloaded from an email attachment or from a web host.

Is there some way to make these associations and app launchings in b4a? When I attach my datafile type to an email and send it to my phone, clicking on the attachment gives me the options of saving (either to the internal memory or the sd card) or opening. When I choose open, I am presented with a list of apps that appear to be geared toward opening a txt file.

Of course, this is not what I want to do. Instead, when a user receives an email with one of my data files attahced, I would like the user to be presented with the option of opening the file with my app which will programmatically open and display the data.

Any ideas?

- - Dave
 

NJDude

Expert
Licensed User
Longtime User
You will have to create an "Open With", add this to your manifest:
B4X:
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>)

The code above assumes you are handling text files in the Main activity, you will have to replace "Main" for the name of your activity and also the MIME type if it's other than plain text.
 
Upvote 0

Dave Fitzpatrick

Member
Licensed User
Longtime User
I appreciate the prompt reply, but I am afraid I have to ask for some clarification.

When you say I have to create an "Open With", could you explain what that means?

Also, if the file type I am trying to associate with the app is of type .xyz, is the mime type just
<data android:mimeType="xyz" />
 
Upvote 0

T0mee

Member
Licensed User
Longtime User
You will have to create an "Open With", add this to your manifest:
B4X:
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>)

The code above assumes you are handling text files in the Main activity, you will have to replace "Main" for the name of your activity and also the MIME type if it's other than plain text.

Hello.
I've added this to my manifest and its working like it should :icon_clap:
But what do i need to add to my source to work with the txt-file?
Sorry, i am very new to b4a and android ...the intent-thing isnt easy to understand for me...

I tried :
B4X:
Sub Service_Start(startingIntent As Intent)
   Log(startingIntent)
End Sub
But nothing seems to happen?!
 
Last edited:
Upvote 0

murdoch1976

Member
Licensed User
Longtime User
I appreciate the prompt reply, but I am afraid I have to ask for some clarification.

When you say I have to create an "Open With", could you explain what that means?

Also, if the file type I am trying to associate with the app is of type .xyz, is the mime type just
<data android:mimeType="xyz" />

Sorry to bump this, but I too am trying to do what Dave has asked. I want to launch my app (or at least have my app listed) when someone tries to open a .babc file (which is essentially a zip file with the extension changed)

So when it comes to this line...

<data android:mimeType="image/*" />

... what would I put please?
 
Upvote 0

murdoch1976

Member
Licensed User
Longtime User
Thanks Erel, that's useful.

Does that mean that I could use my app to create a compressed (zip) file, and also use my app to change the mime type of the file to, for example "application/myapp"?
 
Upvote 0

murdoch1976

Member
Licensed User
Longtime User
Right then, so here goes :)

My app shares a file via email with the file extension (.babc), and associates a mime type of "application/babc".

I have added the following text to the manifest of my project...

AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/babc" />
</intent-filter>)

However, when I try to open a .babc file from an email attachment in Gmail app I get the message...

"No app can open this attachment for viewing"

... if I download the attachment and attempt to launch directly from the device I get the message...

"No apps can perform this action".

Can anyone think of what I'm doing wrong please?
 
Upvote 0

murdoch1976

Member
Licensed User
Longtime User
Erel, you're absolutely correct regarding the Gmail app. I cannot open the attachment directly from email, but if I save it and then open it with a file browser - it launches my app with a decent startingintent.

For anyone that finds it useful, these are the four ways I use to get Android to recognise that a file with a ".babc" extension can be launched via my app - either by mimetype association, or simply by the filename extension...

B4X:
'*******************************************************
'*** FILTER INTENTS BASED ON HTTP REQUESTS
'*******************************************************
'Handle intent from http request simply based on file extension
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" />
<data android:host="*" />
<data android:pathPattern=".*\\.babc" />
<data android:mimeType="*/*" />
</intent-filter>)

'Handle intent from http reqeust based on mimetype
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" />
<data android:host="*" />
<data android:pathPattern=".*\\.*" />
<data android:mimeType="application/myappmimetype" />
</intent-filter>)

'*****************************************************
'*** FILTER INTENTS BASED ON CLICKING A BROWSED FILE
'*****************************************************
'Handle intent from a file click simply based on file extension
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="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.babc" />
<data android:mimeType="*/*" />
</intent-filter>
)

'Handle intent from a file click based on mimetype
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="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.*" />
<data android:mimeType="application/myappmimetype" />
</intent-filter>)
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
Erel, you're absolutely correct regarding the Gmail app. I cannot open the attachment directly from email, but if I save it and then open it with a file browser - it launches my app with a decent startingintent.

For anyone that finds it useful, these are the four ways I use to get Android to recognise that a file with a ".babc" extension can be launched via my app - either by mimetype association, or simply by the filename extension...

B4X:
'*******************************************************
'*** FILTER INTENTS BASED ON HTTP REQUESTS
'*******************************************************
'Handle intent from http request simply based on file extension
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" />
<data android:host="*" />
<data android:pathPattern=".*\\.babc" />
<data android:mimeType="*/*" />
</intent-filter>)

'Handle intent from http reqeust based on mimetype
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" />
<data android:host="*" />
<data android:pathPattern=".*\\.*" />
<data android:mimeType="application/myappmimetype" />
</intent-filter>)

'*****************************************************
'*** FILTER INTENTS BASED ON CLICKING A BROWSED FILE
'*****************************************************
'Handle intent from a file click simply based on file extension
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="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.babc" />
<data android:mimeType="*/*" />
</intent-filter>
)

'Handle intent from a file click based on mimetype
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="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.*" />
<data android:mimeType="application/myappmimetype" />
</intent-filter>)


Hey, I tried cutting and pasting your code to my app manifest. Changed application/myappmimetype to application/babc. Created a file with that extension and when I try and open it I get "No viewer for this type of document"

Is there more to this than just the code here?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is the expected method.
You need to set intent-Filters for your app.

For any more question please create a new thread in the questions-forum (for each question)
 
Upvote 0
Top