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:

hvkaniti

Member
Licensed User
Longtime User
Thank you sir. Could you suggest me if you have any websites or online documentation in your mind as a reference? That helps me a lot.
 

hvkaniti

Member
Licensed User
Longtime User
Thank you very much for your guidance Sir. I am working hard to learn this subject and come up with a library.
 

lorebarita

Member
Licensed User
Longtime User
Getting a list of videos from a specific folder

Hi...I am developing an application and i want to get a list of videos from a specific folder in the SDcard. Am using the MediaBrowser library but am not able to acccomplish that...please help :sign0085:...
 

Informatix

Expert
Licensed User
Longtime User
Hi...I am developing an application and i want to get a list of videos from a specific folder in the SDcard. Am using the MediaBrowser library but am not able to acccomplish that...please help :sign0085:...

Get the video list as I do in the demo and filter the list to keep only the files located in the specific folder. You can filter with a condition like:
B4X:
Dim Location as String
Location = m.Get("Location" & i)
if Location.StartsWith(folder_path) then
...
 

stefanoa

Active Member
Licensed User
Longtime User
hi,
some news about a new release of mediabrowser library that implement other ID3 tags?
At moment i use mediabrowser for my app, but for every song i have to read ID3 tags (genres, etc.) with ID3 class , but ID3 class is very slow...

B4X:
MB.Initialize("MB")
Dim m As Map
m = MB.GetMediaAudioList(True, "artist COLLATE UNICODE, title COLLATE UNICODE" )
....
For i = 0 To (m.Size / 9) - 1
       xLocation=GetParentPath(m.Get("Location" & i))
       xSongfilename=GetFileName(m.Get("Location" & i))

       ID3.Initialize(xLocation, xSongfilename)   '<<<< VERY SLOW
       If ID3.Exists = True Then 
           GenreName=ID3.Genre
       End If
       list1.Add(GenreName)
next
.......

thanks
 

Informatix

Expert
Licensed User
Longtime User
hi,
some news about a new release of mediabrowser library that implement other ID3 tags?
At moment i use mediabrowser for my app, but for every song i have to read ID3 tags (genres, etc.) with ID3 class , but ID3 class is very slow...

I'm very busy currently and most of my free classes and libraries are on pause. But expect something before the end of this month.
 

Informatix

Expert
Licensed User
Longtime User
Finally, no version this month. Mainly because the audio genre is not stored in the MediaStore. I won't release a new version just for the composer field (very rarely used in MP3 tags).

@Stefanoa
Your only way currently to get the missing fields is to use the ID3 class.
 

Yeshua

Member
Licensed User
Longtime User
copy mp3 files with specific title

Hi, maybe you can help me.

I have hundreds of MP3 files in "DirAssets" folder, but I want to copy only those that have the title "SUN".

I tried this way but it does not work. In this way mp3 files are copied all.
You can do this? Thank you!

B4X:
Sub Button1_Click
Dim MyList As List
Dim MyFile As String
Dim i As Short
Dim Title As ID3               :Title.Title = "SUN"
Dim FilePathSource As String        :FilePathSource=File.DirAssets
Dim FilePathDest As String          :FilePathDest=File.DirRootExternal & "/Audio"
   MyList.Initialize                   
    MyList=File.ListFiles(FilePathSource)
    For i=0 To MyList.Size-1
    MyFile=MyList.Get(i)
   If MyFile.Contains(Title)=False  Then
   Try
   File.MakeDir(File.DirRootExternal, "/Audio")
    File.Copy(FilePathSource,MyFile,FilePathDest,MyFile)
    Catch
    End Try
    End If
    Next
End Sub

ID3 (Class Module)

B4X:
Sub Class_Globals
Public Title As String
End Sub
:sign0085:
 

Informatix

Expert
Licensed User
Longtime User
Hi, maybe you can help me.

I have hundreds of MP3 files in "DirAssets" folder, but I want to copy only those that have the title "SUN".

I tried this way but it does not work. In this way mp3 files are copied all.
You can do this? Thank you!

B4X:
Sub Button1_Click
Dim MyList As List
Dim MyFile As String
Dim i As Short
Dim Title As ID3               :Title.Title = "SUN"
Dim FilePathSource As String        :FilePathSource=File.DirAssets
Dim FilePathDest As String          :FilePathDest=File.DirRootExternal & "/Audio"
   MyList.Initialize                   
    MyList=File.ListFiles(FilePathSource)
    For i=0 To MyList.Size-1
    MyFile=MyList.Get(i)
   If MyFile.Contains(Title)=False  Then
   Try
   File.MakeDir(File.DirRootExternal, "/Audio")
    File.Copy(FilePathSource,MyFile,FilePathDest,MyFile)
    Catch
    End Try
    End If
    Next
End Sub

ID3 (Class Module)

B4X:
Sub Class_Globals
Public Title As String
End Sub
:sign0085:

What's the relationship with the MB library ? You should ask your question in the ID3 class thread. I don't use this class so I don't know how it works and what you can do with it. However, by reading your code, I see you do exactly the contrary of what you expect: you copy all files except those with the required title.
 

DavidLen

Member
Licensed User
Longtime User
compile failes

Hi

I'm using b4a 2.3

I'm trying to compile the demo, and it fails on line 11: Dim MB As MediaBrowser

I have copied the MediaBrowser JAR and XML files to the 'additional libraries' folder, and it did not help, then I copied them to the root folder of the project and it still fails.

What am I doing wrong?

David L.
 

Informatix

Expert
Licensed User
Longtime User
Hi

I'm using b4a 2.3

I'm trying to compile the demo, and it fails on line 11: Dim MB As MediaBrowser

I have copied the MediaBrowser JAR and XML files to the 'additional libraries' folder, and it did not help, then I copied them to the root folder of the project and it still fails.

What am I doing wrong?


David L.

You did nothing wrong. They must be copied into the additional libraries folder. Did you reload the B4A example after that and check that the library is selected in your libs tab ?
 

DavidLen

Member
Licensed User
Longtime User
You did nothing wrong. They must be copied into the additional libraries folder. Did you reload the B4A example after that and check that the library is selected in your libs tab ?

Thank you, that was the missing part, I appreciate the help.

David L.
 

Dave O

Well-Known Member
Licensed User
Longtime User
Another very useful library, thanks!

I just used it to fetch random images from the device's mediastore.

I did notice that I occasionally got NumberFormatExceptions when reading the width using map.get ("expected double, got NULL"). I don't know if that's a bug or (more likely) some bad meta-data stored for the image.

BTW, I'm trying to pull photos that match the current orientation of the device (i.e. portrait pictures when in portrait mode). I don't think I can determine this from the dimensions of the photo. Is there any way to do this, either through meta-data or (I doubt) programmatically?

Thanks again for the great library!
 

Informatix

Expert
Licensed User
Longtime User
I did notice that I occasionally got NumberFormatExceptions when reading the width using map.get ("expected double, got NULL"). I don't know if that's a bug or (more likely) some bad meta-data stored for the image.

Width and Height are not reliable. They are null with pre-Honeycomb OS versions. Use the GetImageDimensions function instead.
You have to read the Exif data of a jpeg to know its right orientation. There's a library available for B4A (JpegUtils).
 

enrico

Active Member
Licensed User
Longtime User
I think this lib can't get files from a website. Is there a solution ?
 

Mrjoey

Active Member
Licensed User
Longtime User
hello mr informatix , glad to use ur great library , its really fast , i have some suggestion if u have time for updates :
it seems ur using the native mediaplayer class , i've done my own wrapper for this class and it succeeded to load the cover bitmap of the mp3 file by using :
B4X:
@ShortName("MetaData")
public static class MetaD {
     MediaMetadataRetriever Meta = new MediaMetadataRetriever();
         public void LoadFile(String file){
            Meta.setDataSource(file);
           }
public byte[] Picture(){
             return Meta.getEmbeddedPicture();
         }[CODE]
but my wrapper is pretty slow while retreiving informations i dont know why.
i suggest u to include this line cz i need to display covers in the list.
i also suggest to make a sub for path , to make an option wish folder to search ,and thank u so mush for ur efforts .
 
Top