Android Question File.Copy(xui.DefaultFolder, "NameFile", File.DirRootExternal, "NameFile") not work for android 9

vmag

Active Member
Helo,
not work for android 9:
B4X:
File.Copy(xui.DefaultFolder, "NameFile", File.DirRootExternal, "NameFile")
on android 5, 6, everything works if there are lines in the manifest:
B4X:
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
on android 9, it doesn't help, is there a way out?
Thanks!
Manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="31"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
Button1 code:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Button1 As B4XView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    If File.Exists(xui.DefaultFolder, "input_output.txt") = False Then
        File.Copy(File.DirAssets, "input_output.txt", xui.DefaultFolder, "input_output.txt")
    End If
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    Try
        File.Copy(xui.DefaultFolder, "input_output.txt", File.DirRootExternal, "input_output.txt")
        xui.MsgboxAsync("Ok!","Ok!")
    Catch
        xui.MsgboxAsync("File not copy!","Error!")
    End Try
End Sub
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
You cant use DirRootExternal on newer android versions

 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
not work for android 9
sure it will not work

Check

You can not access File.DirRootExternal any longer (in higher androids)
 
Upvote 1

vmag

Active Member
I solved the problem in principle.
I'm using an old branch File.DirRootExternal, but in case of an error, I choose a different path from the example from here.
In total , the code turned out to be:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Button1 As B4XView
    Private ion As Object
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    If File.Exists(xui.DefaultFolder, "input_output.txt") = False Then
        File.Copy(File.DirAssets, "input_output.txt", xui.DefaultFolder, "input_output.txt")
    End If
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    Try
        File.Copy(xui.DefaultFolder, "input_output.txt", File.DirRootExternal, "input_output.txt")
        xui.MsgboxAsync("Ok!","Ok!")
    Catch
        Try
            File.WriteString(File.DirInternal, "test.txt", "test")
            Wait For (SaveFile(File.OpenInput(File.DirInternal, "test.txt"), "application/octet-stream", "test.txt")) Complete (Success As Boolean)
        Catch
            xui.MsgboxAsync("File not copy!","Error!")
        End Try
    End Try
End Sub

Sub SaveFile (Source As InputStream, MimeType As String, Title As String) As ResumableSub
    Dim intent As Intent
    intent.Initialize("android.intent.action.CREATE_DOCUMENT", "")
    intent.AddCategory("android.intent.category.OPENABLE")
    intent.PutExtra("android.intent.extra.TITLE", Title)
    intent.SetType(MimeType)
    StartActivityForResult(intent)
    Wait For ion_Event (MethodName As String, Args() As Object)
    If -1 = Args(0) Then 'resultCode = RESULT_OK
        Dim result As Intent = Args(1)
        Dim jo As JavaObject = result
        Dim ctxt As JavaObject
        Dim out As OutputStream = ctxt.InitializeContext.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(jo.RunMethod("getData", Null)))
        File.Copy2(Source, out)
        out.Close
        Return True
    End If
    Return False
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub
Thank you so much to everyone who gives a kick in the ass!
 
Upvote 0

vmag

Active Member
I beg your pardon
B4X:
Private Sub Button1_Click
    Try
        File.Copy(xui.DefaultFolder, "input_output.txt", File.DirRootExternal, "input_output.txt")
        xui.MsgboxAsync("Ok!","Ok!")
    Catch
        Try
            Wait For (SaveFile(File.OpenInput(xui.DefaultFolder, "input_output.txt"), "application/octet-stream", "input_output.txt")) Complete (Success As Boolean)
        Catch
            xui.MsgboxAsync("File not copy!","Error!")
        End Try
    End Try
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…