Android Question java.lang.SecurityException: Permission Denial: reading

jvrh_1

Member
Licensed User
Hello, I have a problem.
When I execute my app with a phone that has got a android 9 version, it is all ok. But I tried to do the same with an older phone that has got a 5.0.1 android version, the app crash.

I only want to read a photo of the gallery and copy it in another directory to use it later.
It is a problem with permisission, but I do not know that I have to do. I have read a lot of threads but I do not get to it work. For example this one: https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/#content

My code is it:
B4X:
Sub EventoGaleriaFotos_Result (Success As Boolean, Dir As String, FileName As String)
    Log("EventoGaleriaFotos_Result Success: " & Success)
   dim imgsel as string
   dim dirsel as string
 
    If Success = True Then
        Imgsel = FileName
        DirSel = Dir
               
        Private rp As RuntimePermissions
        Dim ruta As String
        ruta=rp.GetSafeDirDefaultExternal("temp")
       
        If File.Exists(ruta,"ORIGINAL")=True Then
            File.Delete(ruta,"ORIGINAL")
        End If
        File.Copy(DirSel,Imgsel,ruta,"ORIGINAL") ''-->CRASH
       
    Else
        Return ' no se seleccionó imagen
    End If
   
End Sub

My manifest editor has got it:
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="9" android:targetSdkVersion="28"/>
<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.

SetApplicationAttribute(android:usesCleartextTraffic, "true")


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

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

In attached image is the error.
I don not care where to copy the file, just that the file is available.
Please help. I think that it should be easy, but I haven't got what is happening.
 

Attachments

  • ERROR.png
    ERROR.png
    30.1 KB · Views: 61
Last edited:

jvrh_1

Member
Licensed User
I have changed some lines and now I cand detect when I haven't got permission, but How can I offer to the user to give permission when It is neccesary?

I have added this lines:

B4X:
Sub EventoGaleriaFotos_Result (Success As Boolean, Dir As String, FileName As String)
    Log("EventoGaleriaFotos_Result Success: " & Success)
   dim imgsel as string
   dim dirsel as string
 
    If Success = True Then
        Imgsel = FileName
        DirSel = Dir
              
        Private rp As RuntimePermissions
        Dim ruta As String
        ruta=rp.GetSafeDirDefaultExternal("temp")
      
        If File.Exists(ruta,"ORIGINAL")=True Then
            File.Delete(ruta,"ORIGINAL")
        End If
      
       rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
      
    Else
        Return ' no se seleccionó imagen
    End If
  
End Sub

B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)


    Dim resultado As String
    
    If Permission = rp.PERMISSION_READ_EXTERNAL_STORAGE Then
        resultado= Result
    End If
    
    Log(resultado)
    
    If resultado=true then
        ''Do something
    else
        'Show dialog to the user can let the acction. I do not know how do it
    end if
 
Upvote 0
Top