Android Question Read (WhatsApp/Media/WhatsApp Video) Files

carlos7000

Well-Known Member
Licensed User
Longtime User
Hello.

I want to be able to read or access the files inside the folder WhatsApp/media/WhatsApp Video

but when trying to get a list of the files in that folder, with this code, I can't.

Manifest

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-permission
  android:name="android.permission.READ_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)
'to receive share
AddActivityText(Main,
<intent-filter>
   <action android:name="android.intent.action.SEND" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="*/*" />
</intent-filter>)
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

I run the test code in Activity_Resume

B4X:
Sub Activity_Resume
    Activity.LoadLayout("Layout")
    
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Log("What... Video: " & File.IsDirectory(File.DirRootExternal, "WhatsApp/Media/WhatsApp Video"))
    Else
        Log("No hay permiso de lectura")
    End If
    
    Try
        Dim Archivos As List
        Archivos.Initialize
            
        Archivos = File.ListFiles(File.DirRootExternal & "WhatsApp/Media/WhatsApp Video")
        Log("Size " & Archivos.Size)
    Catch
        Log(LastException)
    End Try
        
    Log(Archivos.Size)
End Sub

I get the following error

(RuntimeException) java.lang.RuntimeException: Object should first be initialized (List).
 

KMatle

Expert
Licensed User
Longtime User
You can select a contact and then export chat (with/without media). Then select share to your app.

Good here: Dir.Internal ist used for everything. Parse the files and do your stuff.

Manifest
B4X:
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)


CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

AddActivityText(Main,
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/*" />
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/*" />
</intent-filter>)

Code:
B4X:
Sub Activity_Resume
    Log(Activity.GetStartingIntent.ExtrasToString)
    If Activity.GetStartingIntent.Action="android.intent.action.MAIN" Then
        Return 'App is started via icon (must be started with 'share to'
    End If

    FileList=GetUrisFromStartingIntent(Activity.GetStartingIntent)
    If FileList.Size>0 Then
        For i=0 To FileList.Size-1
            Dim uri As String = FileList.Get(i)
            If uri.Contains(".vcf") = False Then
            Try
                File.Copy("ContentDir",uri,File.DirInternal & "/shared" ,"chat.txt")
                MsgboxAsync("WhatsApp-Chat received and stored (" & File.Size(File.DirInternal & "/shared" ,"chat.txt")&" Bytes)" & CRLF&CRLF ,"Note")
                    CreatePDFsBTN.Visible=True
            Catch
                Log(LastException)
            End Try
            End If
        Next
    End If
    
End Sub

Private Sub GetUrisFromStartingIntent(in As Intent) As List
    Log(in.Action)
    Log(in.ExtrasToString)
    
    Dim FileList As List
    FileList.Initialize
    If in.IsInitialized And in <> OldIntent Then
        If in.Action = "android.intent.action.SEND" Or in.Action= "android.intent.action.SEND_MULTIPLE" Then
            FileList= GetImageUrisFromExtras(in.ExtrasToString, in.action)
        End If
        OldIntent = in
    End If
    Return FileList
End Sub

Sub GetImageUrisFromExtras (Extras As String, Intent As String) As List
    Log(Extras)
    Dim FileList As List
    FileList.Initialize
    
    Dim JO As JavaObject = Activity.GetStartingIntent
    Select Intent
        Case "android.intent.action.SEND"
            Dim uri As String = JO.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
            FileList.Add(uri)
        Case "android.intent.action.SEND_MULTIPLE"
            Dim uris As List = JO.RunMethod("getParcelableArrayListExtra", Array("android.intent.extra.STREAM"))
            If uris.Size > 0 Then
                For i = 0 To uris.Size-1
                    FileList.Add(uris.Get(i))
                Next
            End If
    End Select
    
    Return FileList
End Sub
 
Upvote 0
Top