App/DataFile Association

Discussion in 'Android Questions' started by Dave Fitzpatrick, Oct 29, 2012.

  1. Dave Fitzpatrick

    Dave Fitzpatrick Member Licensed 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
     
  2. NJDude

    NJDude Expert Licensed User

    You will have to create an "Open With", add this to your manifest:
    Code:
    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.
     
  3. Dave Fitzpatrick

    Dave Fitzpatrick Member Licensed 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" />
     
  4. T0mee

    T0mee Member Licensed User

    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 :
    Code:
    Sub Service_Start(startingIntent As Intent)
       
    Log(startingIntent)
    End Sub
    But nothing seems to happen?!
     
    Last edited: Feb 15, 2013
  5. JesseW

    JesseW Active Member Licensed User

  6. T0mee

    T0mee Member Licensed User

    Thank you very much :)

    Now i got it!
     
  7. murdoch1976

    murdoch1976 Member Licensed User

    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?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

  9. murdoch1976

    murdoch1976 Member Licensed 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"?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Files do not have a mime type. When an application sends an intent it can (and should in some cases) set the intent mime type. You can register your application to listen to intents with a specific mime type.
     
  11. murdoch1976

    murdoch1976 Member Licensed User

    Thanks again Erel. That's perfect.
     
  12. murdoch1976

    murdoch1976 Member Licensed 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?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    The problem is most probably that the Gmail client doesn't set the mime type of this file.
     
  14. murdoch1976

    murdoch1976 Member Licensed 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...

    Code:
    '*******************************************************
    '*** 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>)
     
    aeropic likes this.
  15. Gary Milne

    Gary Milne Active Member Licensed User


    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?
     
  16. Gearcam

    Gearcam Active Member Licensed User

    I know this is an old thread but is this still the best method or has it been updated ?
     
  17. DonManfred

    DonManfred Expert Licensed 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)
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice