Android Tutorial On Intents opening files

Just thought I would post a few things I have learned about intents. Using them does not seem to be particularly intuitive and information is sparse. Here is a link to a thread someone started for a listing of KNOWN INTENTS. I hope you add to it as you find them. It also contains links to other sites sharing intents.

http://www.b4x.com/android/forum/threads/list-of-known-android-intents-to-do-stuff.9823/#post-179663

I have posted working intents for Word, Excel, and pdf there.

First of all getting the file path right is not intuitive at all - at least for this old Windows programmer. It seems to require THREE (///) forward slashes after the file: by the time all is said and done (File.DirDefaultExternal comes with 1 / already).

Beyond that, if there is more than one app to open a file here is what I have found on new my Nexus7 With QuickOffice and Adobe reader both loaded. (Adobe was installed After QuickOffice). I tested this by clearing all Launch by Default settings from both QuickOffice and Adobe Reader. Running these, then setting the default to Quick Office and running them again.

If there is more than one application that can open a file and a default has not been set (At least I found this true for pdf files with Adobe and QuickOffice both available:
This code will open the Chooser IF THE DEVAULT HAS NOT BEEN SET and allow you to SET THE DEFAULT. If you set the default it will use it.
B4X:
i.Initialize(i.ACTION_VIEW, "file://" & File.DirDefaultExternal & "/713-042124-001.pdf")
i.SetType("application/pdf")
StartActivity(I)
This code will ALWAYS open the Chooser IF THERE IS MORE THAN ONE PROGRAM but WITHOUT the Option of setting the default even if it is NOT set.
B4X:
    i.Initialize(i.ACTION_VIEW, "file://" &  File.DirDefaultExternal & "/713-042124-001.pdf")
    i.SetType("application/pdf")
    i.WrapAsIntentChooser("Choose PDF Viewer")
    StartActivity(i)
This code will open the FILE DIRECTLY WITH ADOBE READER - NO CHOOSER even if no default is set!
B4X:
    i.Initialize(i.ACTION_VIEW, "file://" &  File.DirDefaultExternal & "/713-042124-001.pdf")
    StartActivity(i)
'This code will ALWAYS OPEN WITH ADOBE READER even if you add
i.WrapAsIntentChooser("Choose PDF Viewer")[/CODE]
B4X:
i.Initialize2("file://" & File.DirDefaultExternal & "/713-042124-001.pdf", 0)
StartActivity(I)

Hope this helps. If someone has installed QuickOffice AFTER Adobe reader I would be curious if these results change. I wasn't willing to spend that much time testing.
 
Last edited:
Top