Android Question Detect USB Storage

yaniv hanya

Active Member
Licensed User
Hi.
Is there a way to detect that a disc on key has been inserted into the USB input
Then automatically view the file log in it (a list as in the External Storage sample)
And if not automatically then maybe with the click of a button. The main thing is to go directly to disk without going through the internal file system
 

rosippc64a

Active Member
Licensed User
Longtime User
You have to write into manifest:
B4X:
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.intent.action.MEDIA_MOUNTED" />
    <data android:scheme="file"/>
</intent-filter>)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.intent.action.MEDIA_REMOVED" />
    <data android:scheme="file"/>
</intent-filter>)
Then you have to make a service named "s1", because this name was in manifest. This will sense the mount/unmount event:
B4X:
#Region  Service Attributes
    #StartAtBoot: False

'#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    If(StartingIntent.Action = "android.intent.action.MEDIA_MOUNTED") Or _
        (StartingIntent.Action = "android.intent.action.MEDIA_REMOVED") Then
        Log("Media mounted/removed. Intent: " & StartingIntent.ExtrasToString)
        Dim jo As JavaObject = StartingIntent
        Dim StorageVolume As JavaObject = jo.RunMethod("getParcelableExtra",Array As Object("android.os.storage.extra.STORAGE_VOLUME"))
        Log("Volume ID:    " & StorageVolume.RunMethod("getUuid", Null))
        Dim context As JavaObject
        context.InitializeContext
        Log("Volume label: " & StorageVolume.RunMethodJO("getDescription", Array As Object(context)))
        CallSubDelayed3("Main","MASS_read",StartingIntent.action,StartingIntent.ExtrasToString)
    End If
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub
and finally in the main there is a sub named "MASS_read":
B4X:
Sub MASS_read(action As String, extra As String)
   dim dnev as string = ""
    'manage usb device volume name
    Dim devid As String = RegexReplace("([a-zA-Z-öüóőúéáűíÖÜÓŐÚÉÁŰÍ].*) (\([0-9a-zA-Z-öüóőúéáűíÖÜÓŐÚÉÁŰÍ].*\))", extra.SubString(extra.IndexOf(":")+1), "$1 ¤ $2")
    Dim deva() As String = Regex.split("¤",devid)
    If deva.Length = 2 Then
        deva(0) = deva(0).Trim 'szöveges név
        deva(1) = deva(1).Replace("(","").Replace(")","").Replace("}","").Replace("]","").Trim 'ID
        dnev = deva(0)
        GetFolderContents3("/storage")
    End If
End Sub
where in the getfoldercontents3 you can examine the connected device.
This works for me in android devices.
 
Upvote 0

yaniv hanya

Active Member
Licensed User
Thanks. It really works great.
But (sorry for the ignorance) how can I check the content
(What's in getfoldercontents3 )
And more precisely if I find out, say, that there is a file there that I find its url
 
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
This is a recursive file ("reklamtv.xml") searching function (android 7.1.2):
B4X:
Sub GetFolderContents3 (fldr As String) As Int

    Dim res As Int = 0
    Try
        If fldr.EndsWith("/") = False Then fldr = fldr & "/"
        Dim entries As List = File.ListFiles(fldr)
        't96 Mars: emulated, self
        If entries.IsInitialized Then
            For Each x As String In entries
                If File.IsDirectory(fldr, x) Then
                    'Log("dir: " & File.Combine(fldr, x))
                    GetFolderContents3(File.Combine(fldr, x))
                Else
                    'Log("file: " & File.Combine(fldr, x))
                    If x.EqualsIgnoreCase("reklamtv.xml") Then
                        File.Copy(fldr,x,File.DirInternal,x)
                        'processing xml file
                        xmlfeldolgozas(x)
                    End If
                End If
            Next
        End If
    Catch
        Log(LastException.Message)
        Log("Error in listing directory. " & fldr & " " & LastException.Message)
    End Try
    Return res
End Sub
 
Last edited:
Upvote 0
Top