Android Question Sharing files from your app with File Provider

Multiverse app

Active Member
Licensed User
Longtime User
I am sorry, but I don't get how to use code in https://www.b4x.com/android/forum/t...om-your-app-with-file-provider.70458/#content to create any other type of intent.
I want to create an intent to set device wallpaper on Android 7
Current code:
B4X:
    Dim i As Intent
    Dim URII As String
    URII = "file://" & File.DirRootExternal&"/WallDrox/Wallpapers/"&strpathextender&"/" & "wall1.png"
    i.Initialize("android.intent.action.ATTACH_DATA", URII)
    i.SetType("image/*")
        StartActivity(i)

Can anyone post the correct code?
 

Multiverse app

Active Member
Licensed User
Longtime User
Do you get any error message?
The code works in pre nougat devices just fine. In Android 7, I get:
(FileUriExposedException) android.os.FileUriExposedException: file:///storage/emulated/0/WallDrox/Wallpapers/
wall1.png exposed beyond app through ClipData.Item.getUri()
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Thanks, it worked. My code:

B4X:
Sub SetWallpaperIntent
    Private sharedFolder As String
    sharedFolder = Starter.rp.GetSafeDirDefaultExternal("shared")

    'copy the file to the shared folder
    File.Copy(File.DirRootExternal&"/WallDrox/Wallpapers/", "wall1.png" , sharedFolder , "wall1.png")
    'The intent
    Dim i As Intent
    i.Initialize("android.intent.action.ATTACH_DATA", CreateFileProviderUri(sharedFolder, "wall1.png"))
    i.SetType("image/*")
    i.Flags = 1
    Try
        StartActivity(i)
    Catch
        ShowSnackBar("Error setting Wallpaper", 0)       
        Log(LastException)
    End Try
End Sub
Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub
 
Upvote 0
Top