Android Question How to Reference a file for Use with ExternalStorage

dank

Member
Licensed User
Longtime User
Hi. I've written an FTP application that copies image files from my phone's internal memory to a hard drive I have attached to my router. For SD card files, I'm trying to use the example program provided by Erel on using ExternalStorage. I understand that output from ListFiles cannot be used directly, but one can use FindFiles instead. However, when I use it, I get the the error "java.io.FileNotFoundException: Camera/1211201928o.jpg (No such file or directory)". What should I use?

B4X:
            Dim folder As ExternalFile = GetCurrentFolder
            Dim f1 As ExternalFile
            f1 = EStorage.FindFile(GetCurrentFolder, f.Name)
            
            ''Dim in As InputStream = EStorage.OpenInputStream(f)
                        
            Dim sf As Object = ftp.UploadFile(folder.Name,f1.Name,False, "/easystore/Backups/" & f1.name)
            Wait For (sf) FTP_UploadCompleted (ServerPath As String, Success As Boolean)
 

agraham

Expert
Licensed User
Longtime User
You can only open an input or output stream on the return from FindFile. For my mapping program which keeps a vast quantity of files on External Storage I copy the file to a temporary location where I can then use normal file handling to play with it.
B4X:
        Dim extfile As ExternalFile = Storage.FindFile(extfolder, Filename)
        If extfile.IsInitialized Then
            ' read and save map file, need to use the commented lines if targeting API 29 or later
            'Dim rp As RuntimePermissions
            'Dim out As OutputStream = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), Starter.MapTempFilename, False)
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, Starter.MapTempDestination, False)         
          
            File.Copy2(Storage.OpenInputStream(extfile), out)
            out.Close
            '...
 
Upvote 0

dank

Member
Licensed User
Longtime User
You can only open an input or output stream on the return from FindFile. For my mapping program which keeps a vast quantity of files on External Storage I copy the file to a temporary location where I can then use normal file handling to play with it.
B4X:
        Dim extfile As ExternalFile = Storage.FindFile(extfolder, Filename)
        If extfile.IsInitialized Then
            ' read and save map file, need to use the commented lines if targeting API 29 or later
            'Dim rp As RuntimePermissions
            'Dim out As OutputStream = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), Starter.MapTempFilename, False)
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, Starter.MapTempDestination, False)        
         
            File.Copy2(Storage.OpenInputStream(extfile), out)
            out.Close
            '...

Thanks!. I was afraid that I'd need to do it that way.
 
Upvote 0
Top