Android Question how to ext. SD Card write on Android 4.4.

monki

Active Member
Licensed User
hi erel,
in Android 4.4 it is only possible to write to the external SD card in a directory whose name Andoid / data / "apk name".
But as it is created?

monki.
 

Filippo

Expert
Licensed User
Thanks for the info.
But is a pity. :(
 
Upvote 0

touchsquid

Active Member
Licensed User
I am running android 4.4.2 on a "stick" computer. Your code above shows only the file.DirDefaultExternal.

The file explore shows the external sdcard as /storage/external_storage/sdcard1

this code:

B4X:
Try
        File.MakeDir("/storage/external_storage/sdcard1", "my.app.package.name")
        Ini.ExtSD = File.Combine("/storage/external_storage/sdcard1", "my.app.package.name")
    Catch
        Msgbox("You need a 16 GB or larger External SD card to run this app!", "No External SD")
        Activity.Finish
        Return
    End Try

Works fine in debug mode. In release mode it seems to work but when I try to write to the folder ini.ExtSD I get this error.

libcore.io.ErrnoException: open failed: EACCES (Permission denied)

Any idea why this doesn't work in release mode? I am able to verify the files are written correctly in debug mode.

Thanks for any suggestions. Using B4A 4.30 Enterprise. <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19"/>
 
Upvote 0

touchsquid

Active Member
Licensed User
OK, I added in the manifest:

AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

and it now works in release mode.

However trying to get the folder with your method fails.
 
Upvote 0

welu1805

Active Member
Licensed User
Hi all,

I use the code from #9 to get the SD card path of my app (de.wedersoft.foo) but it doesn't work.

Although I set #CanInstallToExternalStorage: True, the app is installed in File.DirDefaultExternal (on the internal memory), so the code from #9 only returns File.DirDefaultExternal

iRULU Lollipop 5.1.1
-------------------
The sd card is under /mnt/extsd. There is a folder Android with a subfolder data. In data should be my app folder de.wedersoft.foo, but it isn't there.

UMI C1 KitKat 4.4.2
-------------------
The sd card is under /mnt/sdcard2. There is a folder Android with a subfolder data. In data should be my app folder de.wedersoft.foo, but it isn't there.

Does anyone know what is my problem?

Thanks
 
Last edited:
Upvote 0

welu1805

Active Member
Licensed User
Sorry, I had a mistake in my code.

On iRULU with Lollipop now it works with code from #9. The app directory is created.

But on UMI C1 with KitKat it doesn't work. No app dir is created.

I dont know why. Perhaps the name of the sd card under UMI C1 doesn't contain "ext"?
 
Upvote 0

welu1805

Active Member
Licensed User
I tried to rename /mnt/sdcard2 on UMI C1 to /mnt/extsd with a file manager. But there is an error (translation from german only approximately):

Actual Android version does not support writing on external sd card. You need Android 5 or Root.
 
Upvote 0

DonManfred

Expert
Licensed User
Good day everyone, has anyone tried the code in post #9 with Android Lollipop and Marshmallow versions?
Please start a new Thread in the questions forum for your question...
 
Upvote 0

Tempomaster

Active Member
Licensed User
I would like to use with my app necessarily the external sdcard to the storage of a very large database. After the recherge in this forum and on the Internet I have worked out for me a solution. It should also work on Android 4.4 and later. With the devices samsung s4 mini and s5 mini works the test. I hope for as many confirmations by the members in this superb forum.


B4X:
#Region  Project Attributes
    #ApplicationLabel: Sdcardcheck
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim extroot As String 'The obtained root directory for the actual physical external SD card (user supplied).
    Dim introot As String
    Dim testfolder, subfolder As String
    Dim intern As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
 
    File.MakeDir(File.DirRootExternal, "gd_extsd_test")
    testfolder = "Android/data"
    subfolder = "checkfolder"
    GetSDCardFolder
 
    File.Delete(File.DirRootExternal, "gd_extsd_test")
 
    Msgbox("SDCard intern: " & Chr(10) & introot & Chr(10) & Chr(10) & "SDCard extern: " & Chr(10) & extroot,"Info")
 
    ExitApplication
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GetSDCardFolder
    Dim eintrag As String
    Dim SDf As String
    Dim alldirs As List
    alldirs.Initialize
    Dim subdirs As List
    subdirs.Initialize
 
    alldirs.AddAll(File.ListFiles("/storage"))
    For i= 0 To alldirs.Size-1
        eintrag = alldirs.Get(i)
        If File.IsDirectory("/storage",eintrag) = True Then
            Try
                subdirs.AddAll(File.ListFiles("/storage/" & eintrag))
                If subdirs.IndexOf("gd_extsd_test")>-1 Then
                    intern=True
                Else
                    intern=False
                End If
             
                File.MakeDir("/storage/" & eintrag & "/" & testfolder & "/", subfolder)
                If File.IsDirectory("/storage/" & eintrag & "/" & testfolder & "/", subfolder) Then
                    SDf="/storage/" & eintrag & "/" & testfolder & "/" & subfolder
                    If WriteTest(SDf) Then
                        If intern Then
                            introot="/storage/" & eintrag
                            Log(" ")
                            Log("SDCard intern: " & introot)
                        Else
                            extroot="/storage/" & eintrag
                            Log(" ")
                            Log("SDCard extern: " & extroot)
                        End If
                        File.Delete("/storage/" & eintrag & "/" & testfolder, subfolder)
                        File.Delete("/storage/" & eintrag,testfolder)
                    End If
                End If
            Catch
                Log(LastException.Message)
            End Try
        End If
    Next
End Sub

Sub WriteTest(dirname As String) As Boolean
    Dim s As String
    Try
        File.WriteString(dirname,"test.tmp","test")
        s=File.ReadString(dirname,"test.tmp")
        File.Delete(dirname,"test.tmp")
    Catch
        Log(LastException.Message)
        Return False
    End Try
    If s="test" Then
        Return True
    Else
        Return False
    End If
End Sub

Best regards,
Gunnar Daehling
 
Upvote 0
Top