Android Question FileProvider/Opening files with another Application

MrKim

Well-Known Member
Licensed User
Longtime User
Just thought I would share this. Perhaps it will save someone some time. It is a copy of Erel's FileProvider class. I added a button and code to open several common types of files , the last one (unremmed) is generic for unknown file types, I just used an Excel file as an example. If you just want to cut and paste the code is below for the various file types. This was tested on a Galaxy S7 Edge running Android 8. And a Lenovo tablet running 7.1.1.


B4X:
''********  PDF
'    Log("15-124437-00_a_1.pdf")
'    Dim FileName As String = "15-124437-00_a_1.pdf"
'    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
'    Dim in As Intent
'    in.Initialize(in.ACTION_VIEW, "")
'    Starter.Provider.SetFileUriAsIntentData(in, FileName)
'    'Type must be set after calling SetFileUriAsIntentData
'    in.SetType("application/pdf")
'    StartActivity(in)
''******** END OF PDF

'''********** WORD
'    Log("New Purchase Order Rev 3.docx")
'    Dim FileName As String = "New Purchase Order Rev 3.docx"
'    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
'    Dim in As Intent
'    in.Initialize(in.ACTION_VIEW, "")
'    Starter.Provider.SetFileUriAsIntentData(in, FileName)
'    'Type must be set after calling SetFileUriAsIntentData
'    in.SetType("Application/vnd.ms-word")
'    StartActivity(in)
    '''******* End Of Word

'    ''********** Text
'    Log("TPLink Connect.txt")
'    Dim FileName As String = "TPLink Connect.txt"
'    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
'    Dim in As Intent
'    in.Initialize(in.ACTION_VIEW, "")
'    Starter.Provider.SetFileUriAsIntentData(in, FileName)
'    'Type must be set after calling SetFileUriAsIntentData
'    in.SetType("text/*")
'    StartActivity(in)
'        ''******* End Of Text
   
'''********** HTML
'    Log("kb4344146_20180823_153532367.html")
'    Dim FileName As String = "kb4344146_20180823_153532367.html"
'    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
'    Dim in As Intent
'    in.Initialize(in.ACTION_VIEW, "")
'    Starter.Provider.SetFileUriAsIntentData(in, FileName)
'    'Type must be set after calling SetFileUriAsIntentData
'    in.SetType("text/html")
'    StartActivity(in)
'''******* End Of HTML

''********** Unknown - use extension
    Log("CycleCountInfoDollars.xlsx")
    Dim FileName As String = "CycleCountInfoDollars.xlsx"
    Dim T As String = "CycleCountInfoDollars.xlsx"
    T =  T.SubString(T.LastIndexOf("."))
    T = T.ToLowerCase
    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW, "")
    Starter.Provider.SetFileUriAsIntentData(in, FileName)
    'Type must be set after calling SetFileUriAsIntentData
    in.SetType("application/" & T & "*")
    StartActivity(in)
''******* End Of Unknown - use extension

Make sure you trap any errors. If there is no application available to handle the type you will get:

B4X:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://b4a.example.provider/name/New Purchase Order Rev 3.docx typ=Application/vnd.ms-word flg=0x20001 }
 

Attachments

  • FileProvide-OpenOtherTypes.zip
    441.7 KB · Views: 397
Top