Android Question Another Filewrite Permission question

Hobby4Life

Member
Licensed User
First of all,

I have read the following topics about Persmissions:

https://www.b4x.com/android/forum/threads/android-jar-targetsdkversion-minsdkversion.87610/

and

https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/

But none of them make sense to me how to visualize this into my project.
Adding permission lines to the manifest did not work.

like:

B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="19" />
)


All I want to do is save the following to cfg.txt

B4X:
Sub SaveINI
    Dim List1 As List

    List1.Initialize
   
    List1.Add(Timeslot_Spinner1.SelectedIndex)
    List1.Add(Timeslot_Spinner2.SelectedIndex)
    List1.Add(Timeslot_Spinner3.SelectedIndex)
    List1.Add(Timeslot_Spinner4.SelectedIndex)
    List1.Add(Timeslot_Spinner5.SelectedIndex)
    List1.Add(Timeslot_Spinner6.SelectedIndex)
    List1.Add(Timeslot_Spinner7.SelectedIndex)
    List1.Add(Timeslot_Spinner8.SelectedIndex)
    List1.Add(Timeslot_Spinner9.SelectedIndex)
    List1.Add(Timeslot_Spinner10.SelectedIndex)
    File.WriteList(File.DirRootExternal,FileINI,List1)
End Sub


and get this error.

B4X:
java.io.FileNotFoundException: /storage/emulated/0/cfg.txt (Permission denied)



this is my 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="26"/>
<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.DarkTheme)
'End of default text.
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="19" />
)
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Hobby4Life

Member
Licensed User
As written in the tutorial you need to request the permission when you need it.
go again over

I have been struggling for more than a week with this.. and read it lots of times..

But there is an example with googlemaps.
But I dont see (Visual) how this relates with saving a file.

For some people it is enough to visualize things from text, but unfortunally not for me.

A pinpoint in the good direction would be much apreciated.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Oh yes,

@Hobby4Life , pl check below codes in your project,

B4X:
[INDENT]Sub MapFragment1_Ready
   gmap = MapFragment1.GetMap
   rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
   If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
     gmap.MyLocationEnabled = Result
   End If
End Sub
[/INDENT]

Regards,

Anand
 
Upvote 0

Hobby4Life

Member
Licensed User
I think I got it working :)

My 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="26"/>
<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.DarkTheme)
'End of default text.
AddManifestText(<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
)



B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private pw As PhoneWakeState
    Private timClock As Timer
    Private RP As RuntimePermissions
    Private FileINI As String : FileINI = "cfg.txt"
End Sub



B4X:
Sub SaveINI
    Dim List1 As List

    List1.Initialize
  
    List1.Add(Timeslot_Spinner1.SelectedIndex)
    List1.Add(Timeslot_Spinner2.SelectedIndex)
    List1.Add(Timeslot_Spinner3.SelectedIndex)
    List1.Add(Timeslot_Spinner4.SelectedIndex)
    List1.Add(Timeslot_Spinner5.SelectedIndex)
    List1.Add(Timeslot_Spinner6.SelectedIndex)
    List1.Add(Timeslot_Spinner7.SelectedIndex)
    List1.Add(Timeslot_Spinner8.SelectedIndex)
    List1.Add(Timeslot_Spinner9.SelectedIndex)
    List1.Add(Timeslot_Spinner10.SelectedIndex)
  
    RP.CheckAndRequest(RP.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult( Permission As String, Result As Boolean)
    If Result Then
        File.WriteList(File.DirRootExternal,FileINI,List1)
    Else
  
    End If

End Sub
 
Upvote 0
Top