Android Question Copy file using ExternalStorage

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I don't succeed in copy a file using ExternalStorage.

I do:
B4X:
Sub imvBackup_Click
    Dim Storage As ExternalStorage
    Dim Inp As InputStream
    Dim out As OutputStream
    
    Storage.Initialize(Me,"")
    Storage.SelectDir(False)
    Wait For Storage_ExternalFolderAvailable
    Inp=File.OpenInput(Main.DBFilePath,Main.DBFileName)
    out=Storage.OpenOutputstream(Storage.CreateNewFile(Storage.root,Main.DBFileName))
    Wait For (File.Copy2Async(Inp, out)) Complete (Success As Boolean)
    out.Close
    Inp.Close
'    If(File.Exists(Storage.Root.Name,Main.DBFileName)) Then
        MsgboxAsync("Backup voltooid","")
'    Else
'        MsgboxAsync("Backup mislukt!! Probeer het nogmaals","")
'    End If
End Sub

Can someone help me?

Kind regards,
André
 

GMan

Well-Known Member
Licensed User
Longtime User
Did you enter the permissions in the manifest ?
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
B4X:
AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="31" />
)

Something like this
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
(Main.DBFilePath
What is the content of this Variable?
If you do not let the user select the path: what - do you think - is the path you have access to?
You did not define a presaved path too.

Best is to create a small project which shows the issue!
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
@AHilberink iIs not clear if you solved, if not, use @DonManfred advices,

then try to use this my sub:
B4X:
'Copy a file to External Storage.
'Dir:   The current input file directory, eg. File.DirAssets, DirInternal etc.
'Name:   The current input file name
'Parent:   External Storage parent name (Directory where place a file)
'Rename:   Optionally rename the output file. Pass "" to mantain original name.
Public Sub CopyFileToExternalStorage (Dir As String, Name As String, Parent As ExternalFile, Rename As String) As Boolean
    If File.Exists(Dir, Name) = False Then
        Log("File do not exist while copy to External Storage: " & Name)
        Return False
    End If

    Dim iStream As InputStream, oStream As OutputStream
    Dim oFile As ExternalFile
 
    If Rename <> "" Then
        oFile = FindFileOrCreate(Parent, Rename)  ' Optionally rename output file, pass "" to Rename to mantain original name
    Else
        oFile = FindFileOrCreate(Parent, Name)
    End If
 
    If oFile.IsInitialized = True Then
        iStream = File.OpenInput(Dir, Name)
        oStream = OpenOutputStream(oFile)
        File.copy2(iStream, oStream)
        iStream.Close
        oStream.Close
        Return True
    Else
        Log("Cannot create file on External Storage: " & Name)
        Return False
    End If
End Sub

If you want async you need to adapt, my sub is blocking.

You can find my ExternalStorage Extras class here:
https://www.b4x.com/android/forum/threads/external-storage-extras.143576/

May you solved, if yes, may you can help me here where I want to do it recursively:
https://www.b4x.com/android/forum/t...lstorage-to-dirinternal-and-viceversa.148164/
 
Last edited:
Upvote 0

zed

Active Member
Licensed User
To copy files to the SD card

in the Manifest

AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

Declare the variable "rp"

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public xui As XUI
    Public rp As RuntimePermissions
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    ---
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    
End Sub

B4X:
sub copytoexternal (yourFolder as string, YourFileName as string)

    If File.Exists(rp.GetSafeDirDefaultExternal("yourFolder"),YourFileName) = False Then

        Wait For (File.CopyAsync((File.DirAssets, YourFileName, rp.GetSafeDirDefaultExternal("yourFolder"), YourFileName)) Complete (Success As Boolean)
        If Success Then
            MsgboxAsync("successful copy","")
        Else
            MsgboxAsync("Failed to copy","")
        End If
    end if

end sub
 
Upvote 0
Top