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
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
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
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
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.
Wait For Storage_ExternalFolderAvailable
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
thank youYou cannot access the sd card without letting the user pick the folder (at least once).