Android Question Share image throw FileProvider class

agraham

Expert
Licensed User
Longtime User
Here's how I'm doing it for an image I've saved in File.DirRootExternal/Download

B4X:
    Private Provider As FileProvider ' in Sub Process_Globals
      
    If FirstTime Then ' in Sub Activity_Create
        ....
       Provider.Initialize
       ....
   End If  

Sub btnExtViewer_Click
   ' Provider needs manifest AddApplicationText entry for FileProvider
   ' Provider needs manifest CreateResource entries for both File.DirRootExternal and Download folders
   Provider.SharedFolder = File.DirRootExternal
   Dim intent1 As Intent
   intent1.Initialize(intent1.ACTION_VIEW, Provider.GetFileUri(Starter.MapTempDestination))
   intent1.SetType("image/jpeg")
   intent1.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
   StartActivity( intent1 )
End Sub
You need these manifest entries and seem to need a 'CreateResource' entry for each folder on the path you use
B4X:
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
     android:name="android.support.FILE_PROVIDER_PATHS"
     android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <external-path name="downloads" path="Download" />
)
CreateResource(xml, provider_paths,
   <external-path name="root" path="" />
)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Ah! Thanks for the clarification Erel. I got there (as usual) by trial and error rather than by intellect and erroneously concluded that they added paths cumulatively. So therefore the last entry 'root' gives access to the entire public folder structure on DirRootExternal?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So therefore the last entry 'root' gives access to the entire public folder structure on DirRootExternal?
Not sure at all. Safer to declare all of the paths:
B4X:
CreateResource(xml, provider_paths,
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="downloads" path="Download" />
 <external-path name="root" path="" />
</paths>
)
 
Upvote 0
Top