Android Question Copy Folder from SD to App's folder

ykucuk

Well-Known Member
Licensed User
Longtime User
Hi,

-How can I check is folder exist on SD card.
-How I can copy folder from SD to my application's folder?

thank you
 

ykucuk

Well-Known Member
Licensed User
Longtime User
My Tablet is Samsung T597
Android version is 8.1

I need copy folder from sd card to my safe directory.

Check the attachment for my SD card.

I tried below code.

B4X:
Dim folder As String = "Sentio\Media"

Dim filename As String = "isikliyildiz.png"


Dim extFolder As ExternalFile = Storage.FindFile(Storage.Root, folder)

Log("Folder found: " & extFolder.IsInitialized)

If extFolder.IsInitialized Then

Dim extFile As ExternalFile = Storage.FindFile(extFolder, filename)

Log("File found: " & extFile.IsInitialized)

If extFile.IsInitialized Then

Dim rp As RuntimePermissions

Dim out As OutputStream = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), filename, False)

File.Copy2(Storage.OpenInputStream(extFile), out)

out.Close

End If

End If

there is an error

B4X:
externalstorage_findfile (java line: 125)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4j.object.JavaObject.RunMethod(java.lang.String, java.lang.Object[])' on a null object reference
 

Attachments

  • WhatsApp Image 2018-12-22 at 3.28.01 PM.jpeg
    WhatsApp Image 2018-12-22 at 3.28.01 PM.jpeg
    69.8 KB · Views: 191
Upvote 0

agraham

Expert
Licensed User
Longtime User
This is my routine for doing it. Note that the folders need to be matched one by one. A compound path such as "Sentio\Media" will not work.


B4X:
Private Storage As ExternalStorage
Private UpItem As ExternalFile
...
Storage.Initialize (Me, "Storage")
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
...

Sub CopyMapToDownloadAndOpen(Folders() As String, Filename As String)
   Dim extfolder As ExternalFile
   Dim extfile As ExternalFile
  
   Storage.SelectDir(True)
   Wait For Storage_ExternalFolderAvailable
  
   Dim extfolder As ExternalFile = Storage.FindFile(Storage.Root, Folders(0))
   Log("Folder " & Folders(0) & " found: " & extfolder.IsInitialized)
   If Folders.Length > 1 Then
       For i = 1 To Folders.Length -1
           extfolder = Storage.FindFile(extfolder, Folders(i))
           Log("Folder " & Folders(i) & " found: " & extfolder.IsInitialized)
       Next
   End If
   If extfolder.IsInitialized Then
       Dim extfile As ExternalFile = Storage.FindFile(extfolder, Filename)
       Log("File " & Filename & " found: " & extfolder.IsInitialized)
       If extfile.IsInitialized Then
           Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "/DownLoad/OS Search Map.jpg", False)
           File.Copy2(Storage.OpenInputStream(extfile), out)
           out.Close
       End If
   End If  
  
   Starter.ViewerFilename = Starter.MapTempFilename
   StartActivity(Viewer)
  
End Sub

' Copy "/Maps OS District Jpg/SJ/SJ46.jpg" to temporary file
Sub btnDistrict_Click  
   Starter.ViewerZoom = Starter.MapDistrictZoom
   CopyMapToDownloadAndOPen(Array As String("Maps OS District Jpg", "SJ"), "SJ46.jpg")
End Sub
 
Upvote 0

ykucuk

Well-Known Member
Licensed User
Longtime User
This is my routine for doing it. Note that the folders need to be matched one by one. A compound path such as "Sentio\Media" will not work.


B4X:
Private Storage As ExternalStorage
Private UpItem As ExternalFile
...
Storage.Initialize (Me, "Storage")
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
...

Sub CopyMapToDownloadAndOpen(Folders() As String, Filename As String)
   Dim extfolder As ExternalFile
   Dim extfile As ExternalFile
 
   Storage.SelectDir(True)
   Wait For Storage_ExternalFolderAvailable
 
   Dim extfolder As ExternalFile = Storage.FindFile(Storage.Root, Folders(0))
   Log("Folder " & Folders(0) & " found: " & extfolder.IsInitialized)
   If Folders.Length > 1 Then
       For i = 1 To Folders.Length -1
           extfolder = Storage.FindFile(extfolder, Folders(i))
           Log("Folder " & Folders(i) & " found: " & extfolder.IsInitialized)
       Next
   End If
   If extfolder.IsInitialized Then
       Dim extfile As ExternalFile = Storage.FindFile(extfolder, Filename)
       Log("File " & Filename & " found: " & extfolder.IsInitialized)
       If extfile.IsInitialized Then
           Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "/DownLoad/OS Search Map.jpg", False)
           File.Copy2(Storage.OpenInputStream(extfile), out)
           out.Close
       End If
   End If
 
   Starter.ViewerFilename = Starter.MapTempFilename
   StartActivity(Viewer)
 
End Sub

' Copy "/Maps OS District Jpg/SJ/SJ46.jpg" to temporary file
Sub btnDistrict_Click
   Starter.ViewerZoom = Starter.MapDistrictZoom
   CopyMapToDownloadAndOPen(Array As String("Maps OS District Jpg", "SJ"), "SJ46.jpg")
End Sub

Thank you for your reply. Is there any way for copy folder without user pick? I already know the folder name("sentio/media") at External Storage. How can I get folder name as i need not compound .
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The code above does not need the user to pick any file. I too already know the file and folder names, in the example above they are hard-coded. If you are asked to pick a folder it is the ExternalStorage class seeking permission to access the SD Card. You should select the root folder of the SD Card to enable access to the whole card.
 
Upvote 0

ykucuk

Well-Known Member
Licensed User
Longtime User
The code above does not need the user to pick any file. I too already know the file and folder names, in the example above they are hard-coded. If you are asked to pick a folder it is the ExternalStorage class seeking permission to access the SD Card. You should select the root folder of the SD Card to enable access to the whole card.

if I add below code there is a dialog for pick folder. Check the attachment for dialog screen. Code working well after picking the root. I don't want this dialog shown.
B4X:
Wait For Storage_ExternalFolderAvailable

If i dont add that code it gives below error.
B4X:
 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4j.object.JavaObject.RunMethod(java.lang.String, java.lang.Object[])' on a null object reference
 

Attachments

  • Screenshot_20181222-230727_Files.jpg
    Screenshot_20181222-230727_Files.jpg
    86.4 KB · Views: 160
Upvote 0
Top