Android Question problem with writing SD card

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

I have an app that needs write access to the SD-card.

- i've set the proper permissions in the manifest file:

AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

- I use the "special" folder as explained in another post by erel:

Dim ExternalFilesDirs() As Object = pGetContext.RunMethod("getExternalFilesDirs", Array(Null))

This app runs fine on Android 4.1, Android 2.3 on different brands of phones.

It even runned on a Samsung S4. But, after upgrading the OS on this Samsung to Android 5.01, the app stopped working. From the logs i can see that the app has no longer write permission to the external SD-Card.

My question is: what am i doing wrong?
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello Erel,

the part of the code where i find the path to the correct directorie is here. It uses the code you put somewhere on this forum.

'when we arrive at this point, the card is not found => start searching
#If Debug
Log("Watchdog => SD-card not found: start searching")
#End If
Try
'If it works, a higher version of android is used and the access to the external SD-card is limited to special folders.
'Paths will contain a list of the names of those folders
Dim ExternalFilesDirs() As Object = pGetContext.RunMethod("getExternalFilesDirs", Array(Null))
#If Debug
Log("Watchdog => method getExternalFilesDirs works: use this method to find path to files OTP")
#End If
For Each item As Object In ExternalFilesDirs
#If Debug
Log("Watchdog => test path " & item)
#End If
Try
If File.Exists(item, "CONFIG") = True Then
'card found at this location
CodeModule.RootOTP= item
CodeModule.ReadySdCard= True
LogColor("Root OTP= " & CodeModule.RootOTP, Colors.Red)
Return True
End If
Catch
CodeModule.avoidcompilerwarning_EmptyCatchBlock= 0
End Try
Next
Catch
'ignore this error because the lower versions of android do not support those methods
CodeModule.avoidcompilerwarning_EmptyCatchBlock= 0
#If Debug
Log("Watchdog => error ignored while searching SD-card: " & LastException.Message)
#End If
End Try

it produces a path as follows:

Watchdog => SD-card not found: start searching
Watchdog => method getExternalFilesDirs works: use this method to find path to files OTP
Watchdog => test path /storage/emulated/0/Android/data/otp.gcm/files
Watchdog => test path /storage/extSdCard/Android/data/otp.gcm/files
>>>>>>>> RootOTP= /storage/extSdCard/Android/data/otp.gcm/files

When i use the path for reading files, all files are there so the path as such must be correct.
When i want to write to a file on this path, it is then that the error pops up:
Private Sub WriteBlock(FileName As String, BlockNumber As Int, Block() As Byte)
'write the block
Dim Position As Int = BlockNumber * 256
Dim raf As RandomAccessFile
>>>> raf.Initialize(RootOTP, FileName, False) 'ReadOnly set to False
raf.WriteBytes(Block, 0, 256, Position)
raf.Flush
raf.Close()
End Sub

and this is what the logfile says:
java.io.FileNotFoundException: /storage/extSdCard/Android/data/otp.gcm/files/45F13ECAD9DBCD3F: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:117)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize2(RandomAccessFile.java:86)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.Initialize(RandomAccessFile.java:79)
at otp.gcm.codemodule._writeblock(codemodule.java:6644)
at otp.gcm.codemodule._storagewriteblocktosdcard(codemodule.java:5506)
at otp.gcm.watchdog._ppurgebuffers(watchdog.java:2399)
at otp.gcm.watchdog._service_start(watchdog.java:3629)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at otp.gcm.watchdog.handleStart(watchdog.java:71)
at otp.gcm.watchdog.onStartCommand(watchdog.java:55)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3307)
at android.app.ActivityThread.access$2200(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1546)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
... 23 more
One remark: a pc is used to create the folder structure and write the files on the SD-card.
It might be that android can see that and regards the folder/files as owned by another app
The problem is on a Samsung model number GT-I9505 with the software updated by Samsung (so not rooted, flashed etc.) to Android version 5.0.1


Thanks for your fast reply,
 
Upvote 0
Top