Android Question FTP and Adroid 8 or 9

AlpVir

Well-Known Member
Licensed User
Longtime User
The following code works perfectly with Android 5 but not with Android 8 or 9
B4X:
File.WriteString(File.DirRootExternal,"msg2.txt","test")
ftp.Initialize("FTP", Host, 21, Username,Password)
ftp.UploadFile(File.DirRootexternal,"msg2.txt", True,  IndirizzoFTP & "/msg2.txt")

I tried using FileProvider, without success
B4X:
Dim FileName As String
FileName="msg2.txt"
ftp.Initialize("FTP", Host, 21, Username,Password)
File.Copy(File.DirRootExternal, FileName, Starter.Provider.SharedFolder, FileName)
ftp.UploadFile(File.DirRootexternal,Starter.Provider.GetFileUri(FileName), True, IndirizzoFTP & "/msg2.txt")

I add that FileProvider also uses it in another part of the app and works perfectly.
B4X:
 ' WhatsApp
        Dim FileName As String = NomeImmagine
        File.Copy(File.DirRootExternal, FileName, Starter.Provider.SharedFolder, FileName) 
        '----
        i.Initialize(i.ACTION_SEND, "")
        i.PutExtra("android.intent.extra.TEXT", "Orario " & Titolo)
        i.SetType("text/plain")
        i.PutExtra("android.intent.extra.STREAM", Starter.Provider.GetFileUri(FileName))
        i.SetType("image/*")
        Dim jo As JavaObject = i
        jo.RunMethod("setPackage", Array("com.whatsapp"))
   
            StartActivity(i)
How to solve?
Thanks in advance
 

JohnC

Expert
Licensed User
Longtime User
Try adding this to the manifest - the problem could be that 8 & 9 don't allow non-SSL connections by default:

B4X:
CreateResourceFromFile(Macro, Core.NetworkClearText)

This line allows non-SSL connections.
 
Last edited:
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
are you using runtimepermission and request permission for this path?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The problem with the FTP method is with this line:

B4X:
ftp.UploadFile(File.DirRootexternal,Starter.Provider.GetFileUri(FileName), True, IndirizzoFTP & "/msg2.txt")

You need to specify the SharedDirectory instead of "File.DirRootExternal", and (correction to my previous post) do NOT use URI for the filename, just use the filename (just make sure the file you want to upload is in the SharedDirectory).

This is code I use to get the SharedDirectory:

B4X:
Sub GetSharedDirectory () As String
    Dim SharedFolder As String
    Dim RP As RuntimePermissions
    Dim p As Phone

    If p.SdkVersion >= 24 Or File.ExternalWritable = False Then
        SharedFolder = File.Combine(File.DirInternal, "shared")
        File.MakeDir("", SharedFolder)
    Else
        SharedFolder = RP.GetSafeDirDefaultExternal("shared")
    End If

    Return SharedFolder
End Sub
 
Last edited by a moderator:
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Same problem
Shd is /data/user/0/alpvir.orario/files/shared
The error is the same java.lang.RuntimeException: Error uploading file.
The code is this
B4X:
Dim Shd As String
Shd=GetSharedDirectory
File.WriteString(Shd,"msg2.txt","test")
ftp.Initialize("FTP", Host, 21, Username,Password)
ftp.UploadFile(Shd,"msg2.txt", True,  IndirizzoFTP & "/msg2.txt")         
ftp.Close

Works with Android 5 but not with Android 8 !!!
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I just finished a project that did a lot of FTP and I know for a fact that it works fine on Android 9.

Try one or both of these things (these are the only difference I can tell between your project and mine):

Add this line to the Manifest:

B4X:
AddPermission("android.permission.READ_EXTERNAL_STORAGE")

And in your FTP upload line, change the "True" to False:

ftp.UploadFile(Shd,"msg2.txt", False, IndirizzoFTP & "/msg2.txt"

The "False" forces Binary mode, and you can send text files in binary mode no problem.
 
Last edited:
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
The READ_EXTERNAL_STORAGE line is already in my Manifest.
Even though it was a TXT file I had also done the test of writing "False" in the writing mode.
Without success !!!
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I did find two other things my app is doing that maybe your app isn't:

Try adding these two lines after you initialize the FTP:

B4X:
FTP.PassiveMode = True
FTP.UseSSL = False
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Nothing !!!
I specify that ftp I declared it in Process_Globals
B4X:
Sub Process_Globals
Dim ftp As FTP
End Sub


B4X:
Dim Shd As String
Shd=GetSharedDirectory
File.WriteString(Shd,"msg2.txt","test")
Sleep(2000)
ftp.Initialize("FTP", Host, 21, Username,Password)
ftp.PassiveMode=True
ftp.UseSSL=False
ftp.UploadFile(Shd,"msg2.txt", False,  IndirizzoFTP & "/msg2.txt")
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
With Android 5 is OK

B4X:
'                    File.WriteString(File.DirRootExternal,"msg2.txt","test")
'                    ftp.Initialize("FTP", Host, 21, Username,Password)
'                       ftp.UploadFile(File.DirRootexternal,"msg2.txt", True,  IndirizzoFTP & "/msg2.txt")         
'                    ftp.Close
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The only thing I can think of is that there is some setting or something on that Android 8 device that is preventing this from working.

Are you using the emulator?

Do you have access to a different 8 or 9 device to test it on?
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Thanks. At the moment I only have one smartphone with Android 5 and one with Android 8. I'll try with an emulator soon.
The latter does not even have an antivirus that could block traffic.
@LucaMs : anche usando DirInternal non funziona
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
That's right. Does not transfer the file

B4X:
File.WriteString(File.DirInternal,"msg2.txt","test")
Sleep(2000)
ftp.Initialize("FTP", Host, 21, Username,Password)
ftp.PassiveMode=True
ftp.UseSSL=False
ftp.UploadFile(File.DirInternal,"msg2.txt", False,  IndirizzoFTP & "/msg2.txt")
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
That's right. Does not transfer the file

B4X:
File.WriteString(File.DirInternal,"msg2.txt","test")
Sleep(2000)
ftp.Initialize("FTP", Host, 21, Username,Password)
ftp.PassiveMode=True
ftp.UseSSL=False
ftp.UploadFile(File.DirInternal,"msg2.txt", False,  IndirizzoFTP & "/msg2.txt")
But I'm almost sure that "now" it does not transfer the file even from your device with Android 5, right?
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
With Android 5 and the code that provides for DirInternal the file is transferred.
With Android 8 no.
 
Upvote 0
Top