Sub Class_Globals
Public Root As B4XView
Public xui As XUI
Public Storage As ExternalStorage
Public ExternalAppPath As ExternalFile
Public InternalAppPath As String
Public ES_LibraryPath As ExternalFile
Public ES_FilePath As ExternalFile
Public ES_ImageFilePath As ExternalFile
Public IS_LibraryPath As String
Public IS_FilePath As String
Public IS_ImageFilePath As String
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
'--------------- INITIALIZE EXTERNAL STORAGE -----------------
Storage.Initialize (Me, "Storage")
Wait for (CheckExternalStoragePermission) Complete (result As Boolean) ' If user already gave permission, then continue below...
If result = False Then
Log("ACCESS FILE PERMISSION DANIED")
ToastMessageShow("ACCESS FILE PERMISSION DANIED", False)
MsgboxAsync("ACCESS FILE PERMISSION DANIED. May you deleted the app folder ?" & CRLF & CRLF & "Uninstall the app and try again", " A T T E N T I O N")
Wait For MsgBox_Result (result As Int)
ExitApplication ' Otherwise exit if no permission
Else
Log("ACCESS FILE PERMISSION ALLOWED. APP FOLDER: " & Storage.Root.Name)
ToastMessageShow("ACCESS FILE PERMISSION ALLOWED: " & Storage.Root.Name, True)
End If
'---------------------------------------------
ExternalAppPath = Storage.Root
InternalAppPath = File.Combine(File.DirInternal, Storage.Root.Name)
If Not(Storage.Exists(Storage.Root, "Library")) Then
MessageBox("Successfully created a folder named """ & ExternalAppPath.Name & """ on your device internal memory,"DEAR USER")
End If
' Folders on DirInternal
IS_LibraryPath = File.Combine(InternalAppPath, "Library")
IS_FilePath = File.Combine(InternalAppPath, "Files")
IS_ImageFilePath = File.Combine(InternalAppPath, "Images")
' Find or create folders on external storage
ES_LibraryPath = Storage.FindDirOrCreate(Storage.Root, "Library")
ES_FilePath = Storage.FindDirOrCreate(Storage.Root, "Files")
ES_ImageFilePath = Storage.FindDirOrCreate(Storage.Root, "Images")
'---------- COPY SOME DEFAULT FILES TO EXTERNAL STORAGE SUBFOLDERS ----------
' NOTE: Storage.CopyFileToExternalStorage is a sub I wrote in ExternalStorage
' class. See my ExternalStorage Extras here:
' https://www.b4x.com/android/forum/threads/external-storage-extras.143576/#post-909906
Dim Success As Boolean = False
Dim FileName, FileNameLcase As String
FileName = "VirtualDisplay_1.0.4.zip"
FileNameLcase = FileName.ToLowerCase
If Storage.Exists(ES_LibraryPath, FileName) = False Then
Success = Storage.CopyFileToExternalStorage(File.DirAssets, FileNameLcase, ES_LibraryPath, FileName)
If Success Then
Log("Successfully created the file " & FileName)
Else
LogColor("Error while copy the file " & FileName, Colors.Red)
End If
End If
FileName = "ESP8266-DARK.png"
FileNameLcase = FileName.ToLowerCase
If Storage.Exists(ES_ImageFilePath, FileName) = False Then
Success = Storage.CopyFileToExternalStorage(File.DirAssets, FileNameLcase, ES_ImageFilePath, FileName)
If Success Then
Log("Successfully created the file " & FileName)
Else
LogColor("Error while copy the file " & FileName, Colors.Red)
End If
End If
'-------- IMPORT ----------
Dim Success As Boolean = CopyAllFoldersExternalToInternal(ExternalAppPath, InternalAppPath)
If Success Then
Log("SUCCESSFULLY IMPORTED FOLDER [" & ExternalAppPath.Name & "]")
ToastMessageShow("IMPORT DONE", False)
Else
Log("CANNOT IMPORT FOLDER [" & ExternalAppPath.Name & "]")
ToastMessageShow("IMPORT ERROR", False)
End If
Log("LIST FILES ON DIR INTERNAL AFTER IMPORT:")
For Each ff As String In File.ListFiles(InternalAppPath)
Log("Found on DirInternal: " & ff)
Next
'-------- EXPORT ---------- ' Can do the same to export
' Success = CopyAllFoldersInternalToExternal(InternalAppPath, ExternalAppPath)
'...........
End Sub
Sub CheckExternalStoragePermission As ResumableSub
If Not(Storage.HasPermission) Then
MsgboxAsync("Due to Google Play Store security policy, we show you a file manager, please create a new folder, possibly named 'VirtualDisplay' to use as Application Folder and select it to grant access permissions." & CRLF & CRLF & _
"Next you can use this folder to put/get files in created sub-folders.","Create application folder")
Wait For MsgBox_Result (Result As Int)
End If
Storage.SelectDir(True)
Wait For Storage_ExternalFolderAvailable
Return (Storage.Root.IsFolder And Storage.Root.Name <> "")
End Sub
' IMPORT. Recursively copy all folders and files from ExternalStorage folder to DirInternal folder. (Mantain file structure)
Private Sub CopyAllFoldersExternalToInternal (Source As ExternalFile, targetFolder As String) As Boolean
If Source.IsInitialized = False Or Source.IsFolder = False Then Return False
' Log("COPY [" & Source.Name & "] CONTENT --> " & targetFolder)
For Each f As ExternalFile In Storage.ListFiles(Source)
' Log("File name: " & f.Name)
If f.IsFolder Then
' Log("TEST IF FOLDER EXISTS: " & File.Combine(targetFolder, f.Name))
If File.Exists(targetFolder, f.Name) = False Then File.MakeDir(targetFolder, f.Name) ' THANKS EREL
CopyAllFoldersExternalToInternal (f, File.Combine(targetFolder, f.Name)) ' THANKS EREL
Continue
End If
' External to Internal
Dim iStream As InputStream, oStream As OutputStream
If f.IsInitialized = True And f.Length > 0 Then
Log("IMPORT FILE: [" & f.Name & "] --> " & File.Combine(targetFolder, f.Name))
If LogIsStarted Then
LVLog.AddSingleLine("IMPORT FILE: " & f.Name)
LVLog.SetSelection(LVLog.Size-1)
End If
iStream = Storage.OpenInputstream(f)
oStream = File.OpenOutput(targetFolder, f.Name, False)
File.copy2(iStream, oStream)
iStream.Close
oStream.Close
Else
Log("Cannot open file from external storage")
Return False
End If
Next
Return True
End Sub
' EXPORT. Recursively copy all folders and files from DirInternal folder to ExternalStorage folder. (Mantain file structure)
Private Sub CopyAllFoldersInternalToExternal(Source As String, targetFolder As ExternalFile) As Boolean
'NO YET WRITTEN'
End Sub