Android Question [CLOSED] ExternalStorage Class/Library

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

Trying to update Apps before Google changes access to "File.DirRootExternal".
I currently create a folder in DirRootExternal to allow the App and the user to exchange files. [IE Inport/Export].
I have been trying to use Erel's B4A Class ExternalStorage - Access SD cards and USB sticks as a learning example but it is primarily for "REAL" external memory and doesn't seem to fit my needs.

Below is Erel's "Activity Create" modified in an attempt to replicate what my existing Apps do. [Look for the relevant folder and if not there, create it.]
This does not work. Please help.

Create Folder in DirRootExt:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Storage.Initialize (Me, "Storage")
        FoldersStack.Initialize
        UpItem.Initialize
        
        
'Should MakeDir ***************************       
        If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then
Log("No ExferDir")   
            File.MakeDir(File.DirRootExternal&"/ExferDir/", "")
            If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then
Log("Still no ExferDir")
            End If
        End If
**********************************************       
        
    End If
        
    Activity.LoadLayout("1")
End Sub

Thanks in advance
Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Why not use rp.GetSafeDirDefaultExternal? It will be very simple and won't need any permission.


Thanks Erel,
I thought this was going to be defunct after 5th of May.
I will attempt to use GetS..... Haven't really looked at it before. It's now time, I hope it works, Google seem to be taking an agressive approach to external memory.

Regards Roger.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
As I searched I found that I did try GetSafeDirDefaultExternal a couple of years ago for much the same reason. I could not get it to work then, and still can't.



B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Storage.Initialize (Me, "Storage")
        FoldersStack.Initialize
        UpItem.Initialize
        
        If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then
Log("No ExferDir")

            
            File.MakeDir(rp.GetAllSafeDirsExternal("/ExferDir/"))
        
            
            If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then
Log("Still no ExferDir")
            End If
        End If
    End If
        
    Activity.LoadLayout("1")
End Sub

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Why not use rp.GetSafeDirDefaultExternal? It will be very simple and won't need any permission.

Tried every whichway and still no joy.

The code below is messy as I have left in various trys.
The code below is what I was left with after cleaning up.
This "should" all be simple but buggered if I can see it.

Code with trys left in.:
Sub Activity_Create(FirstTime As Boolean)
    
    If FirstTime Then
        Storage.Initialize (Me, "Storage")
        FoldersStack.Initialize
        UpItem.Initialize
        
        '*
        'rp.GetAllSafeDirsExternal("")  '******************
        rp.CheckAndRequest(rp.GetAllSafeDirsExternal(""))
        
        DirS = rp.GetSafeDirDefaultExternal("")
Log("rp.GetAllSafeDirsExternal("") = "&DirS)

        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        Log(Permission)
        Log(Result)
        
        'If Result =  True  Then
        'If Permission =  "android.permission.GetSafeDirDefaultExternal" And Result =  True  Then
        
            If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then               
Log("No ExferDir")
                File.MakeDir(File.DirRootExternal&"/ExferDir/", "")
            
                If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then
Log("Still no ExferDir")
                End If
                
            End If
        
        'End If
        
    End If
        
    Activity.LoadLayout("1")
End Sub






Cleaned up Code
Code as above but cleaned up:
Sub Activity_Create(FirstTime As Boolean)
    
    If FirstTime Then
        Storage.Initialize (Me, "Storage")
        FoldersStack.Initialize
        UpItem.Initialize
        
        '*
        rp.GetAllSafeDirsExternal("")  '******************

        
        DirS = rp.GetSafeDirDefaultExternal("")

        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        
         File.MakeDir(File.DirRootExternal&"/ExferDir/", "")
            
         If File.Exists(File.DirRootExternal&"/ExferDir/", "") = False Then
Log("Still no ExferDir")
          End If
                    
    End If
        


    Activity.LoadLayout("1")
End Sub
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Erel, I understood the first line but I was pretty sure of that part already.

I guess Google will solve the issue 5th of May. [Long day]

Regards Roger

EDIT: Total puzzlement by your comment "If you see File.DirRootExternal in your code then the code is wrong." If have used the following and similar in several apps for several years. If this is wrong, how do you read/write the Download folder? What I am trying to do [and have done] is create a new folder in the same Dir as the Download folder.
B4X:
File.Copy(File.DirAssets,"helpfile.htm",File.DirRootExternal&"/Download/","ConfigulatorHelp.htm")

From my feeble understanding "GetSafeDirDefaultExternal" is some place else.
The bottom line is: In the coming world of restrictions to external memory how does the App and the user exchange files. EG A list of locations to be loaded into a database in the App. Conversely, a database populated by using the App needs to be exported for use in a spreadsheet.
These are common requirements, I can't see how to do this without a common folder in external memory.

Roger
 
Last edited:
Upvote 0
Top