Hello,
this is my first APP. It should transfer pictures from the android device to a WLAN Server. The following code contains the critical part and works:
The manifest is:
It works but..
1. when I write in the manifest: android:targetSdkVersion="34"/> as recommended, it will simply not transfer data without expressing an "error message"
2. While debugging, I had to introduce the part "Sub DisableStrictMode". Otherwise I got the errorr: Network On Main Thread Exception. Now I tried again without this part in the code and it seems to work - I'm confused.
I imagine, there are more correct ways to proceed and would like to improve. I tried to transfer Code from main to Starter, but this seems to not be enough.
Thank you for any help.
Dirk
this is my first APP. It should transfer pictures from the android device to a WLAN Server. The following code contains the critical part and works:
code:
#Region Project Attributes
#ApplicationLabel: PermissionTest
#VersionCode: 1
#VersionName: 1.0
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#AdditionalJar: jcifs-ng-2.1.9
#AdditionalJar: slf4j-api-1.7.25
#AdditionalJar: commons-logging-1.2
#AdditionalJar: bcpkix-jdk15on-1.68
#AdditionalJar: bcprov-jdk15on-1.68
#BridgeLogger: True
Sub Process_Globals
End Sub
Sub Globals
Private btnTest As Button
Private rp As RuntimePermissions
'Dim jcifs As JavaObject
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layouttest")
End Sub
Sub Activity_Resume
Log("✅ Activity_Resume startet")
DisableStrictMode
End Sub
Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub
Sub btnTest_Click As ResumableSub
Sleep(0)
Log("🟢 btnTest_Click startet")
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
Log("🔁 PermissionResult: " & Permission & ", given = " & Result)
If Result = False Then
ToastMessageShow("❌ Access to memory refused.", True)
Return Null
End If
ToastMessageShow("Permission to access given ✅", False)
Try
' Local file source
Dim localPath As String = "/storage/emulated/0/DCIM/Camera/7.jpg"
Dim input As InputStream = File.OpenInput("", localPath)
' destination on NAS via SMB
Dim remoteUrl As String = "smb://192.168.77.120/public/share_dirk/Dirk/bilderziele/ziel1/7.jpg"
' Authentification
Dim auth As JavaObject
auth.InitializeNewInstance("jcifs.smb.NtlmPasswordAuthenticator", Array("user", "PW"))
' Kontext with Authentification
Dim ctxHelper As JavaObject
ctxHelper.InitializeStatic("jcifs.context.SingletonContext")
Dim baseContext As JavaObject = ctxHelper.RunMethod("getInstance", Null)
Dim authContext As JavaObject = baseContext.RunMethodJO("withCredentials", Array(auth))
' initialize destination file
Dim zielDatei As JavaObject
zielDatei.InitializeNewInstance("jcifs.smb.SmbFile", Array(remoteUrl, authContext))
' Output-Stream
Dim output As OutputStream = zielDatei.RunMethod("getOutputStream", Null)
File.Copy2(input, output)
ToastMessageShow("✅ Datei erfolgreich übertragen", False)
Log("✅ Übertragung abgeschlossen.")
Catch
Log("❌ Error: " & LastException)
ToastMessageShow("Error during transmission", True)
End Try
Return Null
End Sub
The manifest is:
Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="32"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
)
SetApplicationAttribute(android:requestLegacyExternalStorage, "true")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:usesCleartextTraffic, "true")
CreateResourceFromFile(Macro, Themes.LightTheme)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
It works but..
1. when I write in the manifest: android:targetSdkVersion="34"/> as recommended, it will simply not transfer data without expressing an "error message"
2. While debugging, I had to introduce the part "Sub DisableStrictMode". Otherwise I got the errorr: Network On Main Thread Exception. Now I tried again without this part in the code and it seems to work - I'm confused.
I imagine, there are more correct ways to proceed and would like to improve. I tried to transfer Code from main to Starter, but this seems to not be enough.
Thank you for any help.
Dirk