B4A Library [Lib] MediaBrowser

Hello,

This library lists the media content (audio, image and video) of the MediaStore (a database containing meta data for all available media on both internal and external storage devices). It includes an audio player and a media scanner (to scan the new media and add them to the MediaStore).

I added new functions to the library esAudioMediaBrowser provided by Lagore, improved a bit the existing ones, and extended the stuff to images and videos.
The demo (a media gallery) needs the libs Reflection & StringUtils. It displays only files stored on your external SD card (but the lib can handle internal files).

This lib does not work with Android versions < Gingerbread (2.3).

List of properties and methods:
  • GetAudioFieldByID (External As Boolean, ID As Long, Field As String) As String
    Returns a field from the MediaStore about the specified audio file.
    Examples of field: FIELD_COMPOSER, FIELD_DATE_MODIFIED, FIELD_MIME_TYPE, FIELD_SIZE...
  • GetAudioFileInfo (Path As String, File As String) As Map
    Returns a Map containing info from the MediaStore about an audio file.
    The fields that are returned are:
    "ID"
    "Title"
    "Album"
    "Artist"
    "Track"
    "Year"
    "Location"
    "DisplayName"
    "Duration" (in ms)
  • GetAudioFileInfoByID (External As Boolean, ID As Long) As Map
    Returns a Map containing info from the MediaStore about an audio file.
    The fields that are returned are:
    "ID"
    "Title"
    "Album"
    "Artist"
    "Track"
    "Year"
    "Location"
    "DisplayName"
    "Duration" (in ms)
  • GetExifAttribute (Location As String, Attribute As String) As String
    Returns the requested attribute from the Exif data of the specified image.
    Location = complete path name for the file.
    The complete list of attributes can be found here: https://developer.android.com/reference/android/media/ExifInterface
  • GetExifLatLong (Location As String, LatLong As Float()) As Boolean
    Stores the latitude and longitude value of the specified image in a float array. Returns False if these Exif data are not available.
    Location = complete path name for the file.
  • GetExtImageFileInfo (Location As String) As Map
    Returns a Map containing info from the MediaStore about an external image file.
    Location = complete path name for the file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Height" (with Android 3.0+)
    "Width" (with Android 3.0+)
    "Size" (in bytes)
  • GetExtVideoFileInfo (Location As String) As Map
    Returns a Map containing info from the MediaStore about an external video file.
    Location = complete path name for the file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Resolution" (can be Null for some files)
    "Size" (in bytes)
  • GetImageDimensions (Location As String) As Map
    Returns a map containing width and height of the specified file.
    Location = complete path name for the file.
  • GetImageFieldByID (External As Boolean, ID As Long, Field As String) As String
    Returns a field from the MediaStore about the specified image file.
    Examples of field: FIELD_DATE_MODIFIED, FIELD_MIME_TYPE, FIELD_ORIENTATION...
  • GetImageFileInfoByID (External As Boolean, ID As Long) As Map
    Returns a Map containing info from the MediaStore about an image file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Height" (with Android 3.0+)
    "Width" (with Android 3.0+)
    "Size" (in bytes)
  • GetImgThumbnailByID (ID As Long, Mini As Boolean) As android.graphics.Bitmap
    Returns the micro thumbnail (96x96) or mini thumbnail (512x384) of an image file (it is generated if it doesn't exist).
  • GetMediaAudioList (External As Boolean, SortCol As String) As Map
    Returns a Map containing a list of all Audio files in the MediaStore which can be sorted.
    The allowed fields for sorting are:
    Null (= FIELD_ID)
    FIELD_ALBUM
    FIELD_ARTIST
    FIELD_COMPOSER
    FIELD_DATA (location)
    FIELD_DATE_ADDED
    FIELD_DATE_MODIFIED
    FIELD_DISPLAY_NAME
    FIELD_DURATION
    FIELD_MIME_TYPE
    FIELD_SIZE
    FIELD_TITLE
    FIELD_TRACK
    FIELD_YEAR
    The fields that are returned are:
    "ID"
    "Title"
    "Album"
    "Artist"
    "Track"
    "Year"
    "Location"
    "DisplayName"
    "Duration" (in ms)
  • GetMediaImageList (External As Boolean, SortCol As String) As Map
    Returns a Map containing a list of all Image files in the MediaStore which can be sorted.
    The allowed fields for sorting are:
    Null (= FIELD_ID)
    FIELD_DATA (location)
    FIELD_DATE_ADDED
    FIELD_DATE_MODIFIED
    FIELD_DATE_TAKEN
    FIELD_DISPLAY_NAME
    FIELD_HEIGHT
    FIELD_MIME_TYPE
    FIELD_ORIENTATION
    FIELD_SIZE
    FIELD_TITLE
    FIELD_WIDTH
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Height" (with Android 3.0+)
    "Width" (with Android 3.0+)
    "Size" (in bytes)
  • GetMediaVideoList (External As Boolean, SortCol As String) As Map
    Returns a Map containing a list of all Video files in the MediaStore which can be sorted.
    The allowed fields for sorting are:
    Null (= FIELD_ID)
    FIELD_DATA (location)
    FIELD_DATE_ADDED
    FIELD_DATE_MODIFIED
    FIELD_DATE_TAKEN
    FIELD_DISPLAY_NAME
    FIELD_MIME_TYPE
    FIELD_RESOLUTION
    FIELD_SIZE
    FIELD_TITLE
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Resolution" (can be Null for some files)
    "Size" (in bytes)
  • GetVideoFieldByID (External As Boolean, ID As Long, Field As String) As String
    Returns a field from the MediaStore about the specified video file.
    Examples of field: FIELD_DATE_ADDED, FIELD_DATE_MODIFIED, FIELD_MIME_TYPE...
  • GetVideoFileInfoByID (External As Boolean, ID As Long) As Map
    Returns a Map containing info from the MediaStore about a video file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Resolution" (can be Null for some files)
    "Size" (in bytes)
  • GetVideoThumbnailByID (ID As Long, Mini As Boolean) As android.graphics.Bitmap
    Returns the micro thumbnail (96x96) or mini thumbnail (512x384) of a video file (it is generated if it doesn't exist).
    There's a known bug on some Android versions with micro thumbnails:
    If the micro thumbnail can't be generated (e.g. video file format not recognized), the result is either null or a random thumbnail. Thus, for video files, it is strongly recommended to check if the mini thumbnail is initialized before trying to get the micro thumbnail.
  • Initialize (EventName As String)
  • MediaAudioPlay (External As Boolean, ID As Int)
    Plays an audio file.
    Raises the "MediaCompleted" event when the file end is reached.
  • MediaIsLooping As Boolean
    Checks whether the player is looping or non-looping.
  • MediaIsPlaying As Boolean
  • MediaLength As Int
    Returns the duration in ms.
  • MediaPause
  • MediaPosition As Int
    Gets the current playback position.
  • MediaResume
  • MediaSeek (position As Int)
    Seeks to specified time position (in ms).
    Raises the event "SeekCompleted" when the position change has been completed.
  • MediaSetLooping (Looping As Boolean)
    Sets the player to be looping or non-looping.
  • MediaSetVolume (leftVolume As Float, rightVolume As Float)
    Sets the volume on this player (and this player only).
    Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.
    This function must be called AFTER MediaAudioPlay.
  • MediaStop
    Stops the playback and releases the allocated resources.
  • ScanNewMedia (Paths As String())
    Requests the media scanner to scan the given files and add them to the MediaStore.
    Raises the "ScanCompleted" event after each file is scanned.
Warning:
There's a known bug on some Android versions with micro thumbnails:
If the micro thumbnail can't be generated (e.g. video file format not recognized), the result is either null or a random thumbnail. Thus, for video files, it is strongly recommended to check if the mini thumbnail is initialized before trying to get the micro thumbnail.


v1.2:
I fixed two bugs.

v1.21:
I removed the excessive log entries.
I removed all the Try/Catch that silently trapped errors.

v1.3:
This version requires Java 7 and B4A v3.82+.
I added 3 functions:
- GetImageFileInfoByID(External As Boolean, ID As Long) As Map;
- GetVideoFileInfoByID(External As Boolean, ID As Long) As Map;
- ScanNewMedia(Paths() As String).
I updated the demo so it works with recent Android versions.

v1.31:
I fixed a mistake in two functions introduced in v1.3.

v1.4:
I fixed an issue with API 30;
I added five functions: GetAudioFieldByID, GetImageFieldByID, GetVideoFieldByID, GetExifAttribute and GetExifLatLong;
I added a few FIELD constants;
I updated the demo to display the following fields: composer, orientation, latitude and longitude.

Enjoy,
Fred
 

Attachments

  • MediaBrowser v1.4.zip
    80.7 KB · Views: 1,027
Last edited:

mlc

Active Member
Licensed User
Longtime User
Did you add the permission to read the SD card? (look at the manifest of my demo)

Hi Frederic,

I added the permission, so it looks my manifest:

B4X:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

but the map, continues return size = 0

I tried with :

Dim extt As String = "/mnt/sdcard/external_sd/dcim/Camera/20141231_235818.jpg"
and
Dim extt As String = "mnt/sdcard/external_sd/dcim/Camera/20141231_235818.jpg"

What you think that might be?

Thanks
 

Informatix

Expert
Licensed User
Longtime User
Hi Frederic,

I added the permission, so it looks my manifest:

B4X:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

but the map, continues return size = 0

I tried with :

Dim extt As String = "/mnt/sdcard/external_sd/dcim/Camera/20141231_235818.jpg"
and
Dim extt As String = "mnt/sdcard/external_sd/dcim/Camera/20141231_235818.jpg"

What you think that might be?

Thanks
Are you able to see this picture with my demo app?
 

mlc

Active Member
Licensed User
Longtime User
I already know what the problem is, is the directory name.
DCIM must be in uppercase.

"/mnt/sdcard/external_sd/DCIM/Camera/20141231_235749.jpg"

Thanks and sorry
 

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello my friend,
I have a question about ScanNewMedia method. Heres is my code:
B4X:
Dim dir As String
dir = File.DirRootExternal & "/Selfie/"
media.ScanNewMedia(dir)
but throws me error while i try compile it:
B4X:
Error compiling program.
Error description: Cannot cast type: {Type=String,Rank=0, RemoteObject=True} to: {Type=String,Rank=1, RemoteObject=True}
Occurred on line: 270
media.ScanNewMedia(dir)
Word: dir
I see that accepts strings, but why thows error?
Thanks for your time.

EDIT: I figured out why comes error:
B4X:
Dim paths() As String = Array As String(File.Combine(dir,filename)) 'files to scan
But at photo gallery still shows broken thumbnail of the deleted images. Any ideas? (Samsung Note 4, ver. 4.4.4)
 
Last edited:

Traiser

Member
Licensed User
Longtime User
hi all,
i have one little problem with MediaCompleted event, only this don't work... it' a bug?
play, stop ecc. works greats.

B4X:
Sub listaudio_MediaCompleted
    Msgbox("test","test")
    listaudio.MediaStop
End Sub

the event is not called

sorry for my english :)

tnx ^^
 

Alisson

Active Member
Licensed User
Hello, Informatix
How can defined the variable with all images?
My exemple:
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: MediaBrowser demo
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public CurrentMediaTab As Int
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim MB As MediaBrowser
    Dim tabMedias As TabHost
    Dim pnlAudio, pnlImage, pnlVideo As Panel
    Dim svAudio, svImage, svVideo As ScrollView
    Dim lstAudio, lstImage, lstVideo As ClsCheckList
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' Initializations
    Activity.LoadLayout("MediaGallery")

    MB.Initialize("MB")
  
    'pnlAudio.Initialize("")
    'tabMedias.AddTabWithIcon2("Audio", CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnson_inactif.png"), 32, 32), _
                                                  'CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnson.png"), 32, 32), pnlAudio)
    pnlImage.Initialize("")
    tabMedias.AddTabWithIcon2("Imagem", CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnpict_inactif.png"), 32, 32), _
                                                  CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnpict.png"), 32, 32), pnlImage)
    'pnlVideo.Initialize("")
    'tabMedias.AddTabWithIcon2("Video", CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnvideo_inactif.png"), 32, 32), _
                                                  'CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnvideo.png"), 32, 32), pnlVideo)
End Sub

Sub Activity_Resume

        RefreshMediaList
  
End Sub

Sub Activity_Pause(UserClosed As Boolean)
    ' Stop playing music
  
End Sub



Sub ReadImages
    ' Read the image list from the MediaStore and populate the ScrollView
    ProgressDialogShow2("Send...", False)
    DoEvents
    Dim m As Map
    Dim extt As String = "/storage/emulated/0/DCIM/Camera/*.jpg"
    m = MB.GetExtImageFileInfo(extt)
    'm = MB.GetMediaImageList(True, "_display_name COLLATE UNICODE")
    lstImage.Initialize(Me, svImage, "", "Image_Click", "", 0)
    lstImage.ExtensionColor = Colors.ARGB(50, 255, 255, 255)
    DoEvents
    For i = 0 To (m.Size / 7) - 1
        Dim bmp As Bitmap
        Dim Location As String
        bmp = MB.GetImgThumbnailByID(m.Get("ID" & i), False)
        lstImage.AddItemNoChkbx(m.Get("ID" & i), m.Get("DisplayName" & i), m.Get("Location" & i), bmp)
      
    Next
    lstImage.ResizePanel
    ProgressDialogHide
End Sub



Sub RefreshMediaList
    ' Stop playing music
  
      
          
                svImage.Initialize(0)
                pnlImage.AddView(svImage, 0, 0, pnlImage.Width, pnlImage.Height)
          
            ReadImages
      
End Sub



Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int) As Bitmap
    Dim r As Reflector
    Dim b As Bitmap
    b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
            Array As Object(Original, Width, Height, True), _
            Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
    Return b
End Sub

I need read all images, after send to one server ftp.

I wait your feedback!

Thanks!
 

Informatix

Expert
Licensed User
Longtime User
Hello, Informatix
How can defined the variable with all images?
My exemple:
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: MediaBrowser demo
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public CurrentMediaTab As Int
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim MB As MediaBrowser
    Dim tabMedias As TabHost
    Dim pnlAudio, pnlImage, pnlVideo As Panel
    Dim svAudio, svImage, svVideo As ScrollView
    Dim lstAudio, lstImage, lstVideo As ClsCheckList
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ' Initializations
    Activity.LoadLayout("MediaGallery")

    MB.Initialize("MB")
 
    'pnlAudio.Initialize("")
    'tabMedias.AddTabWithIcon2("Audio", CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnson_inactif.png"), 32, 32), _
                                                  'CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnson.png"), 32, 32), pnlAudio)
    pnlImage.Initialize("")
    tabMedias.AddTabWithIcon2("Imagem", CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnpict_inactif.png"), 32, 32), _
                                                  CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnpict.png"), 32, 32), pnlImage)
    'pnlVideo.Initialize("")
    'tabMedias.AddTabWithIcon2("Video", CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnvideo_inactif.png"), 32, 32), _
                                                  'CreateScaledBitmap(LoadBitmap(File.DirAssets, "btnvideo.png"), 32, 32), pnlVideo)
End Sub

Sub Activity_Resume

        RefreshMediaList
 
End Sub

Sub Activity_Pause(UserClosed As Boolean)
    ' Stop playing music
 
End Sub



Sub ReadImages
    ' Read the image list from the MediaStore and populate the ScrollView
    ProgressDialogShow2("Send...", False)
    DoEvents
    Dim m As Map
    Dim extt As String = "/storage/emulated/0/DCIM/Camera/*.jpg"
    m = MB.GetExtImageFileInfo(extt)
    'm = MB.GetMediaImageList(True, "_display_name COLLATE UNICODE")
    lstImage.Initialize(Me, svImage, "", "Image_Click", "", 0)
    lstImage.ExtensionColor = Colors.ARGB(50, 255, 255, 255)
    DoEvents
    For i = 0 To (m.Size / 7) - 1
        Dim bmp As Bitmap
        Dim Location As String
        bmp = MB.GetImgThumbnailByID(m.Get("ID" & i), False)
        lstImage.AddItemNoChkbx(m.Get("ID" & i), m.Get("DisplayName" & i), m.Get("Location" & i), bmp)
     
    Next
    lstImage.ResizePanel
    ProgressDialogHide
End Sub



Sub RefreshMediaList
    ' Stop playing music
 
     
         
                svImage.Initialize(0)
                pnlImage.AddView(svImage, 0, 0, pnlImage.Width, pnlImage.Height)
         
            ReadImages
     
End Sub



Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int) As Bitmap
    Dim r As Reflector
    Dim b As Bitmap
    b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
            Array As Object(Original, Width, Height, True), _
            Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
    Return b
End Sub

I need read all images, after send to one server ftp.

I wait your feedback!

Thanks!
Sorry but I don't understand the question. What's wrong with your code ?
 

Alisson

Active Member
Licensed User
I want read all images the path:

/storage/emulated/0/Android/cache/folder/*.jpg

This is my code:

B4X:
Sub ReadImages
    ' Read the image list from the MediaStore and populate the ScrollView
    ProgressDialogShow2("Send...", False)
    DoEvents
    Dim m As Map
    Dim extt As String = "/storage/emulated/0/DCIM/Camera/*.jpg"
    m = MB.GetExtImageFileInfo(extt)
    'm = MB.GetMediaImageList(True, "_display_name COLLATE UNICODE")
    lstImage.Initialize(Me, svImage, "", "Image_Click", "", 0)
    lstImage.ExtensionColor = Colors.ARGB(50, 255, 255, 255)
    DoEvents
    For i = 0 To (m.Size / 7) - 1
        Dim bmp As Bitmap
        Dim Location As String
        bmp = MB.GetImgThumbnailByID(m.Get("ID" & i), False)
        lstImage.AddItemNoChkbx(m.Get("ID" & i), m.Get("DisplayName" & i), m.Get("Location" & i), bmp)
    
    Next
    lstImage.ResizePanel
    ProgressDialogHide
End Sub

Thanks
 
Top