Android Question Permision Denied Error

Oscarin

Member
Hi, I have this code on a sub, but when I call it I get an error
Dim datex As String
DateTime.TimeFormat = "hhmm"
DateTime.DateFormat = "ddMMMyyyy"
datex = "Inv-" & DateTime.Date(DateTime.Now) &"-"& DateTime.Time(DateTime.Now) & ".csv"
Log(datex)

Dim Result As Boolean = True
If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
End If
If Result Then
Dim MyTextWriter As TextWriter
'Line below assumes text File located in rootexternal. It will be created If Not exist
MyTextWriter.Initialize(File.OpenOutput(File.DirRootExternal, datex,True))
Dim xy As Int
MyTextWriter.WriteLine("Inventario," & DateTime.Date(DateTime.Now))
MyTextWriter.WriteLine("Hora," & DateTime.Time(DateTime.Now))
MyTextWriter.WriteLine("Sucursal," & Label3.Text)
MyTextWriter.WriteLine("Descripcion,En Sistema,En Sucursal")
For xy = 0 To FlexGrid1.RowCount - 1
Dim linea As String = FlexGrid1.GetCellValue(xy,0) & "," & FlexGrid1.GetCellValue(xy,1) & "," & FlexGrid1.GetCellValue(xy,2)
MyTextWriter.WriteLine(linea)
Next
MyTextWriter.Close


The error
java.io.FileNotFoundException: /storage/emulated/0/Inv-12May2022-0119.csv: open failed: EACCES (Permission denied)
I gave it permission for storage but still get the error

This is the manifest

ddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
'AddPermission("android.permission.ACCESS_COARSE_LOCATION")
'AddPermission(android.permission.INTERNET)
'AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(android.permission.DEVICE_POWER)
'AddPermission("android.permission.ACCESS_COARSE_UPDATES")
'AddPermission("android.permission.READ_PHONE_STATE")
AddPermission(android.permission.VIBRATE)
AddPermission(android.permission.CAMERA)
AddPermission(android.permission.FLASHLIGHT)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.hardware.camera)
SetApplicationAttribute(android:usesCleartextTraffic, "true")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:largeHeap, "true")
CreateResourceFromFile(Macro, JhsIceZxing1.CaturePortrait)
CreateResourceFromFile(Macro, Themes.LightTheme)
 

agraham

Expert
Licensed User
Longtime User
Please use code tags when posting code. It's the </> icon at the top left of the post editor toolbar. Also better to use quote tags - middle of the toolbar - when posting other text,

You are probably running it on an Android 11 or 12 phone where you can no longer access File.DirRootExternal. If the app is for private use only you can change your target SDK to 28 which should work or use this class

Here are some ways that will work for SDK 30
 
Upvote 2

Oscarin

Member
Please use code tags when posting code. It's the </> icon at the top left of the post editor toolbar. Also better to use quote tags - middle of the toolbar - when posting other text,

You are probably running it on an Android 11 or 12 phone where you can no longer access File.DirRootExternal. If the app is for private use only you can change your target SDK to 28 which should work or use this class

Here are some ways that will work for SDK 30
Thanks for the reply, I'll try your suggestion.
Also, is there a way to specify the folder on to where to save my output file?
 
Upvote 0
Top