Mobile native Wearable native

Media Content: Using Album, Bookmark, Playlist, and Other Media Content

This tutorial demonstrates how you can manage media content.

Warm-up

Become familiar with the Media Content API basics by learning about:

Follow-up

Once we have learned the basics of the Media Content API, we can now move on to more advanced tasks, including:

Initializing Media Content

To use the media database:

  1. Functions in this module use an SQLite database to save the metadata of the media files on your device, along with the tags and bookmarks that the user sets. To use the functions and data types of the Media Content API (in mobile and wearable applications), include the <media_content.h> header file in your application:

    #include <media_content.h>
    #include <glib.h>
    

    The example application used in the other use cases also requires the <glib.h> header file.

  2. Connect to the database:
    int ret = MEDIA_CONTENT_ERROR_NONE;
    
    ret = media_content_connect();
    if (ret == MEDIA_CONTENT_ERROR_NONE)
       dlog_print(DLOG_DEBUG, LOG_TAG, "connection is success");
    else
       dlog_print(DLOG_ERROR, LOG_TAG, "connection is failed");
    
    return ret;
    
  3. When you do not use the database anymore, disconnect from it:

    media_content_disconnect();
    

Getting the Folder List

To retrieve a list of folders where the media files are stored, register a callback function for each API to manage an individual item in the callback function:

  1. Register a callback function to get the folder list:
    bool gallery_folder_list_cb(media_folder_h folder, void *user_data)
    {
       media_folder_h new_folder = NULL;
       media_folder_clone(&new_folder, folder);
    
       GList **list = (GList**)user_data; // Include glib.h for this value 
    
       *list = g_list_append(*list, new_folder);
    
       return true;
    }
    
  2. Get the folder list:
    int i;
    int count;
    filter_h filter = NULL;
    GList *folder_list = NULL; // Include glib.h for this value 
    media_folder_h folder_handle = NULL;
    
    ret = media_folder_foreach_folder_from_db(filter, gallery_folder_list_cb, &folder_list);
    if (ret != MEDIA_CONTENT_ERROR_NONE) {
       dlog_print(DLOG_ERROR, LOG_TAG, "media_folder_foreach_folder_from_db failed: %d", ret);
    
       return ret;
    } 
    else 
    {
       dlog_print(DLOG_DEBUG, LOG_TAG, "media_folder_foreach_folder_from_db success!!");
    
       char *folder_id = NULL;
       char *folder_name = NULL;
       char *folder_path = NULL;
    
       for (i = 0; i < g_list_length(folder_list); i++) 
       {
          folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
    
          ret = media_folder_get_folder_id(folder_handle, &folder_id);
    
          ret = media_folder_get_name(folder_handle, &folder_name);
    
          ret = media_folder_get_path(folder_handle, &folder_path);
    
          ret = media_folder_get_media_count_from_db(folder_id, filter, &count);
    
          // Release allocated strings 
          free (folder_id);
          free (folder_name);
          free (folder_path);
          if (ret != MEDIA_CONTENT_ERROR_NONE) 
          {
             dlog_print(DLOG_ERROR, LOG_TAG, "media_folder_get_media_count_from_db failed: %d", ret);
    
             return ret;
          } 
          else 
          {
             dlog_print(DLOG_DEBUG, LOG_TAG, "media count [%d] : %d", i, count);
          }
       }
    }
    

Getting the Item List

To retrieve the item list, use a foreach function with a previously defined callback:

  1. Create a callback function to retrieve a media item:
    bool gallery_media_item_cb(media_info_h media, void *user_data)
    {
       media_info_h new_media = NULL;
       media_info_clone(&new_media, media);
    
       GList **list = (GList**)user_data; // Include glib.h for this value 
       *list = g_list_append(*list, new_media);
    
       return true;
    }
    
  2. To find media items, use the media_info_foreach_media_from_db() function with a filter to filter and sort the results. For a detailed list of condition fields (such as MEDIA_TYPE) and their values (such as MEDIA_CONTENT_TYPE_IMAGE and MEDIA_CONTENT_TYPE_VIDEO), see the media_content_type.h header file.
    #define BUFLEN 200 
    
    // Get the item list 
    media_info_h media_handle = NULL;
    GList *all_item_list = NULL;
    
    media_content_collation_e collate_type = MEDIA_CONTENT_COLLATE_NOCASE;
    media_content_order_e order_type = MEDIA_CONTENT_ORDER_DESC;
    ret = media_filter_create(&filter);
    
    snprintf(buf, BUFLEN, "%s = %d OR %s = %d", MEDIA_TYPE, MEDIA_CONTENT_TYPE_IMAGE, 
             MEDIA_TYPE, MEDIA_CONTENT_TYPE_VIDEO);
    ret = media_filter_set_condition(filter, buf, collate_type);
    if (ret != MEDIA_CONTENT_ERROR_NONE) 
    {
       media_filter_destroy(filter);
       dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set condition");
    
       return ret;
    }
    
    ret = media_filter_set_order(filter, order_type, MEDIA_DISPLAY_NAME, collate_type);
    if (ret != MEDIA_CONTENT_ERROR_NONE) 
    {
       media_filter_destroy(filter);
       dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set order");
    
       return ret;
    }
    
    ret = media_info_foreach_media_from_db(filter, gallery_media_item_cb, &all_item_list);
    

Destroying the Handle

To destroy the handle received from each function (to release resources):

// Remove the folder list 
if (folder_list) 
{
   for (i = 0; i < g_list_length(folder_list); i++) 
   {
      folder_handle = (media_folder_h)g_list_nth_data(folder_list, i);
      media_folder_destroy(folder_handle);
   }

   g_list_free(folder_list);
}

// Remove all items list 
if (all_item_list) 
{
   for (i = 0; i < g_list_length(all_item_list); i++) 
   {
      media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
      ret = media_info_destroy(media_handle);
   }

   g_list_free(all_item_list);
}

// Destroy handle of the filter
media_filter_destroy(filter);

Receiving Update Notifications

To get notifications of database changes, register a callback. You can only set 1 notification callback at this stage of the process:

  1. Define the database update callback function:
    void _noti_cb(media_content_error_e error, int pid,
                  media_content_db_update_item_type_e update_item,
                  media_content_db_update_type_e update_type,
                  media_content_type_e media_type,
                  char *uuid, char *path, char *mime_type, void *user_data)
    {
       if (error == MEDIA_CONTENT_ERROR_NONE) 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "noti success! : %d\n", error);
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "error occurred! : %d\n", error);
       }
    
       dlog_print(DLOG_DEBUG, LOG_TAG, "Noti from PID(%d)\n", pid);
    
       if (update_item == MEDIA_ITEM_FILE) 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Noti item : MEDIA_ITEM_FILE\n");
       } 
       else if (update_item == MEDIA_ITEM_DIRECTORY) 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Noti item : MEDIA_ITEM_DIRECTORY\n");
       }
    
       if (update_type == MEDIA_CONTENT_INSERT) 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Noti type : MEDIA_CONTENT_INSERT\n");
       } 
       else if (update_type == MEDIA_CONTENT_DELETE) 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Noti type : MEDIA_CONTENT_DELETE\n");
       } 
       else if (update_type == MEDIA_CONTENT_UPDATE) 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Noti type : MEDIA_CONTENT_UPDATE\n");
       }
    
       dlog_print(DLOG_DEBUG, LOG_TAG, "content type : %d\n", media_type);
    
       if (path) dlog_print(DLOG_DEBUG, LOG_TAG, "path : %s\n", path);
       if (uuid) dlog_print(DLOG_DEBUG, LOG_TAG, "uuid : %s\n", uuid);
       if (mime_type) dlog_print(DLOG_DEBUG, LOG_TAG, "mime_type : %s\n", mime_type);
       if (user_data) dlog_print(DLOG_DEBUG, LOG_TAG, "String : %s\n", (char *)user_data);
    
       return;
    }
    
  2. Register the callback function:
    int ret = MEDIA_CONTENT_ERROR_NONE;
    
    // Subscribe notifications 
    char *user_str = strdup("hi");
    media_content_set_db_updated_cb(_noti_cb, (void*)user_str);
    
  3. When you no longer want to receive notifications, unregister the database update callback function:
    media_content_unset_db_updated_cb();
    

Initializing the Album Module

To use the functions and data types of the Media Album API (in mobile and wearable applications), include the <media_content.h> header file in your application:

#include <media_content.h>

Before using the Album module, open a connection to the Content Service by calling the media_content_connect() function:

media_content_connect();

When the module is no longer needed, close the connection:

media_content_disconnect();

Finding All Albums

To find all albums in the system and access information about the album content:

  1. Find all albums.

    Call the media_album_get_album_count_from_db() and media_album_foreach_album_from_db() functions to receive information about albums. Set the first parameter to NULL to retrieve all albums and perform no filtering.

    The media_album_list_cb()callback is called for each found album.

    media_album_foreach_album_from_db(NULL, media_album_list_cb, NULL);

    Note that the function is synchronous. The call blocks until the callback is called for all albums or the callback returns false.

  2. Retrieve album information.

    Define the callback and retrieve the basic album information (album id, name, the artist name, and the number of media items in the album) in it.

    If the callback returns true, the iteration continues and the callback is called for the next album, if available. If the value is false, the iteration stops.

    bool media_album_list_cb(media_album_h album, void *user_data)
    {
       media_content_error_e ret = MEDIA_CONTENT_ERROR_NONE;
    
       int id = -1; 
       char *name = NULL, *artist = NULL;
       int count = -1;
    
       // Get the ID of the album
       ret = media_album_get_album_id(album, &id);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling	
       }
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Album id: %d\n", id);
       }   
    
       // Get name of the album
       ret = media_album_get_name(album, &name);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       }
       else 
       {
           dlog_print(DLOG_DEBUG, LOG_TAG, "Album name: %s\n", name);
          free(name);
       }   
    
       // Get the artist name
       ret = media_album_get_artist(album, &artist);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Artist: %s\n", artist);
          free(artist);
       }
    
       // Get media count in the album
       // Filter is NULL - all media items are counted
       ret = media_album_get_media_count_from_db(id, NULL, &count);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Media count in this album: %d\n", count);
       }
    
       return true;
    }
    Note
    Free album_name and artist after use.

Finding Albums Using a Filter

Use a filter to find the albums that meet a certain criteria. If an album does not meet the conditions, it is not in the results.

Before searching for the albums, create the filter, and set its conditions and sort order:

#define BUFLEN 200

filter_h filter = NULL;
char buf[BUFLEN] = {'\0'};

media_filter_create(&filter);

snprintf(buf, BUFLEN, "%s = 'Tizen'", MEDIA_ARTIST);
media_filter_set_condition(filter, buf, MEDIA_CONTENT_COLLATE_NOCASE);

media_filter_set_order(filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_DISPLAY_NAME, MEDIA_CONTENT_COLLATE_NOCASE);

media_album_foreach_album_from_db(filter, media_album_list_cb, NULL);

media_filter_destroy(filter);

Retrieving Album Content Information

To access information about the media items in a given album:

  1. Request album content.
    1. Before you can read the album content information, acquire the album ID. If you have the album handle (media_album_h), you can call the media_album_get_album_id() function:

      int id = -1;
      
      media_album_get_album_id(album, &id);

      To obtain the handle, call the media_album_foreach_album_from_db() function. The handle is provided in the callback.

    2. Request the album content and media item count with the media_album_foreach_media_from_db() and media_album_get_media_count_from_db() functions using the album ID as the first parameter.

      The following call finds all the media items in the album (NULL filter). The album_contents_info_cb() callback is called for each item.

      media_album_foreach_media_from_db(id, NULL, album_contents_info_cb, NULL);

      This function is synchronous. The call blocks until the callback is called for all the albums or the callback returns false.

  2. Receive album content.

    Define the callback in which you can receive and handle the album content.

    As long as the callback returns true, the callback is called for the next item, if available. The iteration stops when the return value is false.

    bool album_contents_info_cb(media_info_h media, void *user_data)
    {
       media_content_error_e ret = MEDIA_CONTENT_ERROR_NONE;
    
       char *title = NULL, *mime_type = NULL;
       unsigned long long size = -1;
    
       // Read the item title
       ret = media_info_get_title(media, &title);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Title: %s\n", title);
          free(title);
       }
    
       // Read the item MIME type
       ret = media_info_get_mime_type(media, &mime_type);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "MIME type: %s\n", mime_type);
          free(mime_type);
       }
    
       // Read the item size
       ret = media_info_get_size(media, &size);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Size: %llu\n", size);
       }
    
       return true;
    }
    Note
    Free title and mime_type after use.

Initializing Bookmarks

To initialize the bookmark feature:

  1. To use the functions and data types of the Media Bookmark API (in mobile and wearable applications), include the <media_content.h> header file in your application:

    #include <media_content.h>
    
  2. Before using bookmarks, open a connection to the Content Service by calling the media_content_connect() function.

    media_content_connect();

    Close the connection when the service is no longer needed:

    media_content_disconnect();

Inserting a Bookmark

To set a bookmark for a video file at a given timestamp, use the media_bookmark_insert_to_db() function:

char* thumbnail_path = "path/to/image/file";

media_bookmark_insert_to_db(media_id, 220, thumbnail_path);
media_bookmark_insert_to_db(media_id, 210, thumbnail_path);

The parameters are the media ID of the video file, the moment (time in seconds from the beginning) in the video to bookmark, and the image used as a thumbnail for the bookmark. You can use the same thumbnail for more than 1 bookmark.

Finding Bookmarks

To find a media item's bookmarks and filter the results:

Finding All Bookmarks

To find the bookmarks set for a media item, use the media_info_foreach_bookmark_from_db() function:

media_info_foreach_bookmark_from_db(media_id, NULL, get_bookmarks_cb, NULL);

The media ID, required as the first parameter, can be obtained by calling the *_get_media_id() functions. The handles that these functions require can be obtained from various sources. For example, media_info_h (needed for media_info_get_media_id()) is provided after calling the media_info_foreach_media_from_db() or media_info_insert_to_db() function.

Find the bookmarks satisfying certain criteria or otherwise modify the results with a filter. If the filter parameter is NULL, no filtering is performed and all bookmarks are returned.

The callback is called for each bookmark in the media item. If the callback returns true, calling of the callback continues, if there are more bookmarks for which the callback has not been called yet. If the return value is false, the callback iteration stops.

The media_info_foreach_bookmark_from_db() function is synchronous. It blocks until the callback is called for all bookmarks or returns false.

Finding Bookmarks Using a Filter

To filter the bookmarks, create a filter and set its properties:

  1. Create a filter.
    filter_h filter = NULL;
    
    media_filter_create(&filter);
  2. Set a condition.

    For example, you can receive a bookmark set after a certain time. In this example, only the bookmarks set at the 220th second of the file or later are returned.

    #define BUFLEN 200
    char buf[BUFLEN] = {'\0'};
    
    snprintf(buf, BUFLEN, "%s >= 220", BOOKMARK_MARKED_TIME);
    media_filter_set_condition(filter, buf, MEDIA_CONTENT_COLLATE_DEFAULT);
  3. Set the offset for the filter.
    media_filter_set_offset(filter,
                            0, // Offset
                            3 // Count
    );

    The count sets the limit on the number of returned bookmarks, and the offset determines which of the found bookmarks counts as the "first". For example, if you sort the bookmarks in an ascending order and the offset is set to 1, you do not receive the earliest bookmark.

  4. Define the sorting order.

    The following code causes the filter to sort the results by time, in an ascending order:

    media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, BOOKMARK_MARKED_TIME, MEDIA_CONTENT_COLLATE_NOCASE);
  5. Apply the filter.
    media_info_foreach_bookmark_from_db(media_id, filter, get_bookmarks_cb, NULL);

    For information on how to implement the callback, see the Reading Bookmark Information.

  6. Destroy the filter.

    media_filter_destroy(filter);

Reading Bookmark Information

To read the bookmark information within a callback after finding the bookmark:

bool get_bookmarks_cb(media_bookmark_h bookmark, void* user_data)
{
   char *thumb = NULL;

   if (bookmark != NULL) 
   {
       media_bookmark_get_thumbnail_path(bookmark, &thumb);
       dlog_print(DLOG_DEBUG, LOG_TAG, "Thumbnail path: %s\n", thumb);
       free(thumb);

       time_t time;

       media_bookmark_get_marked_time(bookmark, &time);
       dlog_print(DLOG_DEBUG, LOG_TAG, "Marked time [s]: %d\n", (int) time);
   }

   return true;
}

The above callback prints the thumbnail path and marked time for each found bookmark.

Free the thumbnail path after use.

Removing a Bookmark

To remove a bookmark:

  1. Acquire the bookmark ID:

    media_bookmark_h bookmark = NULL;
    
    // Assuming you have the bookmark handle (media_bookmark_h)
    int bookmark_id = -1;
    media_bookmark_get_bookmark_id(bookmark, &bookmark_id);
  2. Delete the bookmark:

    media_bookmark_delete_from_db(bookmark_id);

Initializing a Filter

To use the functions and data types of the Media Filter API (in mobile and wearable applications), include the <media_content.h> header file in your application:

#include <media_content.h>

Open the connection to the Content Service by calling the media_content_connect() function before using the Media Filter API:

media_content_connect();

When the service is no longer needed, close the connection:

media_content_disconnect();

Setting up a Filter

To create a filter and set its properties: offset, condition, and sorting order.

  1. Create a filter.

    Create a filter using the media_filter_create() function:

    filter_h filter = NULL;
    
    media_filter_create(&filter);
    

    When the filter is no longer needed, destroy it using the media_filter_destroy() function:

    media_filter_destroy(filter);
  2. Set the condition.

    Set the condition using the media_filter_set_condition() function.

    The following code sets the condition which finds albums with the artist name "Tizen", and the condition is case insensitive:

    #define BUFLEN 200
    char buf[BUFLEN] = {'\0'};
    
    snprintf(buf, BUFLEN, "%s = 'Tizen'", MEDIA_ARTIST);
    ret = media_filter_set_condition(filter, buf, MEDIA_CONTENT_COLLATE_NOCASE);

    The function parameters are:

    • Filter handle
    • Condition string used for filtering the search results

      Queries made with the Content API (in mobile and wearable applications) are passed to a SQL database. A part of each query is a WHERE clause that determines which properties correspond to certain conditions. The condition string defines the WHERE clause and must match the following pattern:

      <property> <relation> <value>
      
      // If the relation is =, >, >=, <, or <=, the following is also valid
      <value> <relation> <property>

      The valid relations are:

      =, >, >=, <, <=, IN, NOT IN, LIKE

      Conditions can be joined by OR and AND to form more complex conditions.

      Condition properties are defined by a series of #define directives. Each Content submodule contains definitions for properties used in the submodule.

    • Collation type determining how the strings are compared

      The media_content_collation_e enumerator (in mobile and wearable applications) defines the available collation types.

    To read the condition of an existing filter, use the media_filter_get_condition() function:

    char *check_condition = NULL;
    media_content_collation_e check_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
    
    media_filter_get_condition(filter, &check_condition, &check_collate_type);
  3. Set the sorting order.

    The sorting order is set using the media_filter_set_order() function. For example, the following code sets the order by the artist name, ascending order, and case sensitive.

    media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_ARTIST, MEDIA_CONTENT_COLLATE_DEFAULT);

    The second and fourth parameters determine the order and collation types, and the available types are defined in the enumerators media_content_order_e (in mobile and wearable applications) and media_content_collation_e (in mobile and wearable applications).

    To read the order settings of a filter, use the media_filter_get_order() function:

    media_content_order_e check_order_type = MEDIA_CONTENT_ORDER_ASC;
    char *check_order_keyword = NULL;
    media_content_collation_e check_order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
    
    media_filter_get_order(filter, &check_order_type, &check_order_keyword, &check_order_collate_type);
  4. Set the offset.

    To avoid receiving all results returned by the query, use the media_filter_set_offset() function.

    For example, set the offset to return results starting from the beginning (offset 0), and get a maximum of 5 results:

    media_filter_set_offset(filter, 0, 5);

    You can use the offset to get specific results. For example, if you sort the items by size in an ascending order and set the offset to 10, the 10 smallest items are not included in the results.

    To read the filter offset settings, use the media_filter_get_offset() function:

    int check_offset = 0;
    int check_count = 0;
    
    media_filter_get_offset(filter, &check_offset, &check_count);

Using Filters to Find Media Items

To use a filter to find media items:

The filter construction depends on what kind of a query it is used for. The condition string contains different properties (columns), depending on the submodule.

The following example finds media items with the following conditions:

  • The artist is 'Tizen'
  • The search for the artist is case-insensitive
  • The results are ordered by the artist, ascending, and case-sensitive
  • At most 5 results are provided, starting from the first result
filter_h filter = NULL;
char buf[BUFLEN] = {'\0'};

media_filter_create(&filter);

media_filter_set_offset(filter, 0, 5);
snprintf(buf, BUFLEN, "%s = 'Tizen'", MEDIA_ARTIST);
media_filter_set_condition(filter, buf, MEDIA_CONTENT_COLLATE_NOCASE);
media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_ARTIST, MEDIA_CONTENT_COLLATE_DEFAULT);

media_info_foreach_media_from_db(filter, media_cb, NULL);

media_filter_destroy(filter);

Initializing Media Folders

To use the functions and data types of the Media Folder API (in mobile and wearable applications), include the <media_content.h> header file in your application:

#include <media_content.h>

Open the connection to the Content Service by calling the media_content_connect() function before using the Media Folder API:

media_content_connect();

When the service is no longer needed, close the connection:

media_content_disconnect();

Finding All Media Folders

To list all available media folders and their content:

  1. Request all folders.

    Find folders using the media_folder_foreach_folder_from_db() function. This function finds all folders meeting the criteria given in a filter (when set to NULL, all folders available on the system are returned) and calls the callback for each such folder.

    media_folder_foreach_folder_from_db(NULL, folder_cb, NULL);

    This function is synchronous; it continues until the callback is called for all available folders or the callback returns false.

  2. Read folder details within the callback:
    bool folder_cb(media_folder_h folder, void *user_data)
    {

    The return value determines if the iterative calls of the callback continue (true) or are stopped (false).

    Once you have the folder handle (media_folder_h), you can read the folder information:

    • Use the media_folder_get_folder_id(), media_folder_get_name(), and media_folder_get_path() functions to read the folder ID, name, and path:

         char *folder_id = NULL;
         char *name = NULL;
         char *path = NULL;
      
         ret = media_folder_get_folder_id(folder, &folder_id);
         if (ret != MEDIA_CONTENT_ERROR_NONE) 
         {
            // Error handling
         }
         else 
         {
            dlog_print(DLOG_DEBUG, LOG_TAG, "Folder id: %s\n", folder_id);
         }
      
         ret = media_folder_get_name(folder, &name);
         if (ret != MEDIA_CONTENT_ERROR_NONE) 
         {
            // Error handling
         }
         else 
         {
            dlog_print(DLOG_DEBUG, LOG_TAG, "Folder name: %s\n", name);
            free(name);
         }
      
         ret = media_folder_get_path(folder, &path);
         if (ret != MEDIA_CONTENT_ERROR_NONE) 
         {
            // Error handling
         }
         else 
         {
            dlog_print(DLOG_DEBUG, LOG_TAG, "Folder path: %s\n", path);
            free(path);
         }

      Free name and path at the end. The folder_id is freed later, since it is still needed.

    • Read the folder storage type using the media_folder_get_storage_type() function:

         media_content_storage_e storage_type = MEDIA_CONTENT_STORAGE_INTERNAL;
      
         ret = media_folder_get_storage_type(folder, &storage_type);
         if (ret != MEDIA_CONTENT_ERROR_NONE) 
         {
            // Error handling
         }
         else 
         {
            switch (storage_type) 
            {
               case MEDIA_CONTENT_STORAGE_INTERNAL:
                  dlog_print(DLOG_DEBUG, LOG_TAG, "Folder storage type: Internal\n");
                  break;
               case MEDIA_CONTENT_STORAGE_EXTERNAL:
                  dlog_print(DLOG_DEBUG, LOG_TAG, "Folder storage type: External\n");
                  break;
               default:
                  dlog_print(DLOG_DEBUG, LOG_TAG, "Folder storage type: Unknown\n");
                  break;
            }
         }
    • Get the last modified time:

         time_t time = 0;
      
         ret = media_folder_get_modified_time(folder, &time);
         if (MEDIA_CONTENT_ERROR_NONE != ret) 
         {
            // Error handling
         }
         else 
         {
            dlog_print(DLOG_DEBUG, LOG_TAG, "Modified time: %s", ctime(&time));
         }
    • Get the media item count in the folder with the media_folder_get_media_count_from_db() function.

      The second parameter is the filter. If it is set to NULL, all media is counted.

      Free the folder_id value after it is used for the media_folder_get_media_count_from_db() function.

         int item_count = -1; 
      
         ret = media_folder_get_media_count_from_db(folder_id, NULL, &item_count);
         if (MEDIA_CONTENT_ERROR_NONE != ret) 
         {
            // Error handling
         }
         else 
         {
            dlog_print(DLOG_DEBUG, LOG_TAG, "Number of media contents: %d\n", item_count);
         }
         free(folder_id);
      
         return true;
      }

Finding Folders Using a Filter

Use filters to choose which folders are found by the media_folder_foreach_folder_from_db() function. For example, find folders which meet certain conditions, such as a name containing a given substring. Other modifications of the results, such as sorting, are also possible.

  1. Create a filter.

    Create a filter using the media_filter_create() function:

    filter_h filter = NULL;
    
    media_filter_create(&filter);

    Destroy the filter when it is no longer needed:

    media_filter_destroy(filter);
  2. Set the condition.

    Set the condition with the media_filter_set_condition() function.

    The condition is the string used as the parameter to the WHERE clause in the SQL query. The general format is "<property> <relation> <value>", such as "FOLDER_NAME = 'Downloads'".

    • Condition for the 'Downloads' folder name:
      #define BUFLEN 200
      char buf[BUFLEN] = {'\0'};
      
      snprintf(buf, BUFLEN, "%s = 'Downloads'", FOLDER_NAME);
    • Condition for the internal folder storage type:

      snprintf(buf, BUFLEN, "%s = %d", FOLDER_STORAGE_TYPE, MEDIA_CONTENT_STORAGE_INTERNAL);

    The following code finds the folders in the device internal storage. The results are sorted with respect to the modified time, in an ascending order.

    #define BUFLEN 200
    char buf[BUFLEN] = {'\0'};
    
    snprintf(buf, BUFLEN, "%s = %d", FOLDER_STORAGE_TYPE, MEDIA_CONTENT_STORAGE_INTERNAL);
    
    media_filter_set_condition(filter, buf, MEDIA_CONTENT_COLLATE_NOCASE);
    
    media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, FOLDER_MODIFIED_TIME, MEDIA_CONTENT_COLLATE_DEFAULT);
    
    media_folder_foreach_folder_from_db(filter, folder_cb, NULL);
    
    media_filter_destroy(filter);

Finding Folder Content

To list folder content and get information about media items in the folders:

  1. Request folder content.

    To find all media items from the folder with the given ID, use the media_folder_foreach_media_from_db() function. The function is synchronous; it blocks until the callback calls are finished.

    media_folder_foreach_media_from_db(folder_id, NULL, media_cb, NULL);

    The callback is iterated until it has been called for all items or it returns false.

  2. Receive folder content.

    List the content using the media_folder_foreach_media_from_db() function (this is very similar to using the media_album_foreach_media_from_db() function, since both functions call callbacks of the same type). To implement the callback, see the media_album_foreach_media_from_db() callback implementation.

    The following example prints the title of the media item for which it was called.

    bool media_cb(media_info_h media, void *user_data)
    {
       char *title = NULL;
    
       ret = media_info_get_title(media, &title);
       if (ret != MEDIA_CONTENT_ERROR_NONE) 
       {
          // Error handling
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Title: %s\n", title);
          free(title);
       }   
    
       return true;
    }

Initializing Media Information

To use the media database:

  1. To use the functions and data types of the Media Information API (in mobile and wearable applications), include the <media_content.h> header file in your application:

    #include <media_content.h>
    
  2. Before retrieving media information, open a connection to the Content Service using the media_content_connect() function:

    media_content_connect();
  3. Close the connection when the service is no longer needed:

    media_content_disconnect();

Using Media Information

To retrieve media information:

  1. Create a callback function to retrieve a media item:
    bool gallery_media_item_cb(media_info_h media, void *user_data)
    {
       media_info_h new_media = NULL;
       media_info_clone(&new_media, media);
    
       GList **list = (GList**)user_data;
       *list = g_list_append(*list, new_media);
    
       return true;
    }
    
  2. Create a handle by setting a query and filter.

    Media information is based on the media information handle. The metadata varies depending on the media type, such as image, video, or audio. You can obtain common information from the media information.

    Based on the media information handle, use a getter function for the metadata after calling a clone function supported by each metadata.

    #define BUFLEN 200
    GList *all_item_list = NULL; // Include glib.h
    media_content_type_e media_type;
    media_info_h media_handle = NULL;
    char *media_id = NULL;
    char *media_name = NULL;
    char *media_path = NULL;
    char buf[BUFLEN] = {'\0'};
    int ret = MEDIA_CONTENT_ERROR_NONE;
    filter_h filter = NULL;
    media_content_collation_e collate_type = MEDIA_CONTENT_COLLATE_NOCASE;
    media_content_order_e order_type = MEDIA_CONTENT_ORDER_DESC;
    
    media_filter_create(&filter);
    
    // Set the condition
    snprintf(buf, BUFLEN, "%s = %d OR %s = %d", MEDIA_TYPE, MEDIA_CONTENT_TYPE_IMAGE, MEDIA_TYPE, MEDIA_CONTENT_TYPE_VIDEO);
    
    ret = media_filter_set_condition(filter, buf, collate_type);
    if (ret != MEDIA_CONTENT_ERROR_NONE) 
    {
       media_filter_destroy(filter);
       dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set condition");
    
       return ret;
    }
    ret = media_filter_set_order(filter, order_type, MEDIA_DISPLAY_NAME, collate_type);
    if (ret != MEDIA_CONTENT_ERROR_NONE) 
    {
       media_filter_destroy(filter);
       dlog_print(DLOG_ERROR, LOG_TAG, "Failed to set order");
    
       return ret;
    }
    
    ret = media_info_foreach_media_from_db(filter, gallery_media_item_cb, &all_item_list);
    if (ret != MEDIA_CONTENT_ERROR_NONE) 
    {
       dlog_print(DLOG_ERROR, LOG_TAG, "media_info_foreach_media_from_db failed: %d", ret);
       media_filter_destroy(filter);
    
       return ret;
    } 
    else 
    {
       int i;
    
       for (i = 0; i < g_list_length(all_item_list); i++) 
       {
          media_handle = (media_info_h)g_list_nth_data(all_item_list, i);
    
          media_info_get_media_id(media_handle, &media_id);
    
          media_info_get_media_type(media_handle, &media_type);
    
          media_info_get_display_name(media_handle, &media_name);
    
          media_info_get_file_path(media_handle, &media_path);
    
          if (media_type == MEDIA_CONTENT_TYPE_IMAGE) 
          {
             image_meta_h image_handle;
             media_content_orientation_e orientation = 0;
             int width = 0, height = 0;
             char *datetaken = NULL;
             char *burst_id = NULL;
    
             ret = media_info_get_image(media_handle, &image_handle);
             if (ret != MEDIA_CONTENT_ERROR_NONE) 
             {
                // Error handling
             } 
             else 
             {
                image_meta_get_width(image_handle, &width);
    
                image_meta_get_height(image_handle, &height);
    
                image_meta_get_orientation(image_handle, &orientation);
    
                image_meta_get_date_taken(image_handle, &datetaken);
    
                image_meta_get_burst_id(image_handle, &burst_id);
    
                dlog_print(DLOG_DEBUG, LOG_TAG, "This is an image");
                dlog_print(DLOG_DEBUG, LOG_TAG, "Width : %d, Height : %d, Orientation : %d, Date taken : %s", width, height, orientation, datetaken);
             }
    
             if (datetaken) free(datetaken);
             if (burst_id) free(burst_id);
    
             image_meta_destroy(image_handle);
          } 
          else if (media_type == MEDIA_CONTENT_TYPE_VIDEO) 
          {
             video_meta_h video_handle;
             char *title = NULL, *artist = NULL, *album = NULL, *album_artist = NULL;
             int duration = 0;
             time_t time_played = 0;
    
             ret = media_info_get_video(media_handle, &video_handle);
             if (ret != MEDIA_CONTENT_ERROR_NONE) {
                // Error handling
             } 
             else 
             {
                video_meta_get_artist(video_handle, &artist);
    
                video_meta_get_album(video_handle, &album);
    
                video_meta_get_album_artist(video_handle, &album_artist);
    
                video_meta_get_duration(video_handle, &duration);
    
                video_meta_get_played_time(video_handle, &time_played);
    
                dlog_print(DLOG_DEBUG, LOG_TAG, "This is a video");
                dlog_print(DLOG_DEBUG, LOG_TAG, "Title: %s, Album: %s, Artist: %s, Album_artist: %s \n Duration: %d, Played time: %d", title, album, artist, album_artist, duration, time_played);
             }
    
             free(artist);
             free(album);
             free(album_artist);
    
             video_meta_destroy(video_handle);
          }
          dlog_print(DLOG_DEBUG, LOG_TAG, "media_id [%d] : %s", i, media_id);
          dlog_print(DLOG_DEBUG, LOG_TAG, "media_name [%d] : %s", i, media_name);
          dlog_print(DLOG_DEBUG, LOG_TAG, "media_path [%d] : %s", i, media_path);
    
          free(media_id);
          free(media_name);
          free(media_path);
          free(title);
       }
    }
    
  3. When the filter is no longer used, destroy the list, filter, and query:
    media_filter_destroy(filter);
    filter = NULL;
    

Inserting Media in the Database

To insert media in the database:

  1. To use newly created media files, first insert them into the database. You need the http://tizen.org/privilege/content.write privilege to use the needed APIs.

    You also need the http://tizen.org/privilege/mediastorage or http://tizen.org/privilege/externalstorage privilege, since when you insert metadata, the media content APIs access content in the internal or external storage.

  2. To add information in the database:

    • You can use the media_info_insert_to_db() function:

      int ret = MEDIA_CONTENT_ERROR_NONE;
      char *image_file = "Default.jpg";
      char *directory = NULL;
      char *path = NULL;
      
      storage_get_directory(0, STORAGE_DIRECTORY_IMAGES, &directory);
      path = (char*)malloc(strlen(directory) + strlen(image_file) + 1);
      memset(path, 0x00, strlen(directory) + strlen(image_file) + 1);
      sprintf(path, "%s/%s", directory, image_file);
      
      media_info_h media_item = NULL;
      
      ret = media_info_insert_to_db(path, &media_item);
      
      if ((ret == MEDIA_CONTENT_ERROR_NONE) && (media_item != NULL)) 
      {
         dlog_print(DLOG_DEBUG, LOG_TAG, "Insertion successful");
      } 
      else 
      {
         dlog_print(DLOG_ERROR, LOG_TAG, "Insertion failed");
         if (media_item != NULL)
            media_info_destroy(media_item);
      
         return ret;
      }
      
      // After using the media handle, destroy it
      ret = media_info_destroy(media_item);
      if (ret != MEDIA_CONTENT_ERROR_NONE) 
      {
         // Error handling
      
         return ret;
      }
      
    • You can use the media_content_scan_file() function:
      int ret = MEDIA_CONTENT_ERROR_NONE;
      char *image_file = "Default.jpg";
      char *directory = NULL;
      char *file_path = NULL;
      
      storage_get_directory(0, STORAGE_DIRECTORY_IMAGES, &directory);
      file_path = (char*)malloc(strlen(directory) + strlen(image_file) + 1);
      memset(file_path, 0x00, strlen(directory) + strlen(image_file) + 1);
      sprintf(file_path, "%s/%s", directory, image_file);
      
      ret = media_content_scan_file(file_path);
      if (ret != MEDIA_CONTENT_ERROR_NONE)
      {
         // Error handling
      }
      

    The difference between the 2 functions is that the media_info_insert_to_db() function is used by getting media_info_h of the media file after inserting it to the database, whereas the media_content_scan_file() function supports only inserting a file to the media database, or removing it.

    Basically, the media database does not allow duplicate paths. If you try to insert the same data, the media_content_scan_file() function returns an error. But the media_info_insert_to_db() function does not return an error, and allows you to get media_info_h.

  3. After using the media_info_insert_to_db() function, destroy the received media_info_h with using the media_info_destroy() function.

Inserting Media in a Folder

To insert media in a folder:

  1. You can scan either a single folder, or include its subdirectories. You need the http://tizen.org/privilege/content.write privilege to use these APIs.

    You also need the http://tizen.org/privilege/mediastorage or http://tizen.org/privilege/externalstorage privilege, since when you insert metadata, the media content APIs access content in the internal or external storage.

  2. As scanning works asynchronously, you must a callback function:
    void _scan_cb(media_content_error_e err, void *user_data)
    {
       dlog_print(DLOG_DEBUG, LOG_TAG, "Folder scanned\n");
    
       return;
    }
    
  3. Scan a folder and its subfolders:
    int ret = MEDIA_CONTENT_ERROR_NONE;
    char *dir_path = NULL;
    
    storage_get_directory(0, STORAGE_DIRECTORY_IMAGES, &dir_path);
    
    ret = media_content_scan_folder(dir_path, TRUE, _scan_cb, NULL);
    if (ret != MEDIA_CONTENT_ERROR_NONE) 
    {
       // Error handling
    
       return ret;
    }
    

Initializing a Playlist

To use the playlists:

  1. To use the functions and data types of the Media Playlist API (in mobile and wearable applications), include the <media_content.h> header file in your application:

    #include <media_content.h>
    
  2. Establish a connection to the database:

    media_content_connect();
    

    Close the connection when the service is no longer needed:

    media_content_disconnect();
    

Managing Playlists

To create and insert a playlist to the database, perform a search on existing records and handle the found items:

  1. Insert a record to the database.
    1. To operate on the database, it has to contain records. Add a new playlist to the database using the media_playlist_insert_to_db() function. It returns a handle to the newly created record.

      media_playlist_h playlist = NULL;
      filter_h audio_filter = NULL;
      GList *media_id_list = NULL; // This requires glib.h inclusion
      int media_count;
      int start_position = -1;
      int count = -1;
      int x;
      int id;
      int ret;
      
      // Create the playlist
      media_playlist_insert_to_db("playlist_for_tutorial", &playlist);
      
    2. To fill a playlist with data, register the existing media in the database using the media_content_scan_file() function. Then you can check the media database size using the media_info_get_media_count_from_db() function and allocate the required amount of space. The callback function registered using the media_info_foreach_media_from_db() function returns a list of media IDs.

      The specified media objects are added to the playlist with media_playlist_add_media().

      // sound_path must be set to an audio file
      media_content_scan_file(path); 
      
      media_info_get_media_count_from_db(NULL, &media_count); 
      
      media_info_foreach_media_from_db(NULL, _media_info_cb, &media_id_list); 
      
      if (media_count > 0) 
      {
         for (x = 0; x < media_count; x++) 
         {
            char *media_id = (char*)g_list_nth_data(media_id_list, x);
      
            ret = media_playlist_add_media(playlist, media_id);
      
            free(media_id);
         }
      }
      
      media_playlist_update_to_db(playlist);
    3. In the callback function, cast the user data to a proper type and copy the media ID:

      bool _media_info_cb(media_info_h media, void *user_data)
      {
         char *data = NULL;
         char *id = NULL;
      
         if (media != NULL) 
         {
            GList **list = (GList**)user_data;
      
            media_info_get_media_id(media, &id);
            dlog_print(DLOG_DEBUG, LOG_TAG, "Media found: %s\n", id);
      
            data = strdup(id);
            *list = g_list_append(*list, data);
      
            free(id);
         }
      
         return true;
      }
  2. Perform a search.

    To select any records from the database, you must know their ID or perform a search on the records.

    To start a search, use the media_playlist_foreach_playlist_from_db() function. To use this function, provide an existing filter and a callback function to be used each time a record is found.

    media_playlist_foreach_playlist_from_db(NULL, playlist_list_cb,  NULL);
    

    You can define the playlist name using the media_playlist_set_name() function.

  3. Handle the found records.
    1. When a record is found, a callback function is invoked. A handle to the record is passed as a playlist function parameter.

      static bool playlist_list_cb(media_playlist_h playlist, void* user_data) 
      {
         char *name = NULL;
         int playlist_id = 0;
         int count = 0;
      
    2. Access the record details using the media_playlist_get_name() and media_playlist_get_playlist_id() functions.

         media_playlist_get_name(playlist, &name);
         media_playlist_get_playlist_id(playlist, &playlist_id);
      
    3. After reading the playlist ID, execute 2 searches on it. The first search filter operates on all records on the playlist, and the second one works only on the first 10 records. Destroy the filters after all operations.

      The media_playlist_foreach_media_from_db() function invokes a callback for each record matching the filter. The search is performed on a playlist with the specified ID.

         filter_h temp_filter = NULL;
         media_filter_create(&temp_filter);
      
         media_filter_set_offset(temp_filter, -1, -1);
      
         media_playlist_get_media_count_from_db(playlist_id, temp_filter, &count);
         filter_h audio_fltr;
      
         media_filter_create(&audio_fltr);
      
         media_filter_set_offset(audio_fltr, 0, 10);
      
         media_playlist_foreach_media_from_db(playlist_id, audio_fltr, audio_list_cb, NULL);
      
         media_filter_destroy(audio_fltr);
         media_filter_destroy(temp_filter);
      
         return true;
      }
    4. The callback function operates on the records the same way as before. To get the media info, use the following functions:

      • media_info_get_audio()
      • media_info_get_media_id()
      • media_info_get_size()
      static bool audio_list_cb(int playlist_member_id, media_info_h media_hndl, void *user_data) 
      {
         char * id;
         media_info_get_media_id(media_hndl, &id);
         dlog_print(DLOG_DEBUG, LOG_TAG, "Media on the playlist: %s\n", id);
         free(id);
      
         return true;
      }
  4. Clean up.

    After all operations, delete the playlist from the database to avoid creating useless records. In addition, destroy all filters and handles to the records. At the end, close the connection to the database.

    media_playlist_get_playlist_id(playlist, &id);
    media_playlist_delete_from_db(id);
    
    media_playlist_destroy(playlist);
    
    media_filter_destroy(audio_filter);
    media_content_disconnect();

Initializing Media Tags

To use the functions and data types of the Media Tag API (in mobile and wearable applications), include the <media_content.h> header file in your application:

#include <media_content.h>

Before retrieving tag information, open a connection to the Content Service by calling the media_content_connect() function:

media_content_connect();

Close the connection when the service is no longer needed:

media_content_disconnect();

Getting the Tag List

To get the tag list:

  1. Define callback functions:
    // glib library is used, so include glib.h
    
    bool gallery_tag_item_cb(media_tag_h tag, void *user_data)
    {
       media_tag_h new_tag = NULL;
       media_tag_clone(&new_tag, tag);
    
       GList **list = (GList**)user_data;
       *list = g_list_append(*list, new_tag);
    
       return true;
    }
    
    bool gallery_media_item_cb(media_info_h media, void *user_data)
    {
       media_info_h new_media = NULL;
       media_info_clone(&new_media, media);
    
       GList **list = (GList**)user_data;
       *list = g_list_append(*list, new_media);
    
       return true;
    }
    
  2. After getting the tag information which is already set, you can import individual media information.

    Get a tag and media list using a tag ID:

    media_tag_h tag_handle = NULL;
    GList *tag_list = NULL;
    GList *media_list_in_tag = NULL;
    
    ret = media_tag_foreach_tag_from_db (NULL, gallery_tag_item_cb, &tag_list);
    
    if (ret != MEDIA_CONTENT_ERROR_NONE)
    {
       dlog_print(DLOG_ERROR, LOG_TAG, "media_tag_foreach_tag_from_db() failed: %d", ret);
    
       return ret;
    } 
    else 
    {
       dlog_print(DLOG_DEBUG, LOG_TAG, "media_tag_foreach_tag_from_db() successful");
       char *tag_name = NULL;
       int tag_id;
    
       for (i = 0; i < g_list_length(tag_list); i++) 
       {
          tag_handle = (media_tag_h)g_list_nth_data(tag_list, i);
          media_tag_get_tag_id(tag_handle, &tag_id);
          media_tag_get_name(tag_handle, &tag_name);
    
          ret = media_tag_foreach_media_from_db(tag_id, NULL, gallery_media_item_cb, &media_list_in_tag);
          if (ret != MEDIA_CONTENT_ERROR_NONE) 
          {
             dlog_print(DLOG_ERROR, LOG_TAG, "media_tag_foreach_media_from_db() failed: %d", ret);
    
             return ret;
          } 
          else 
          {
             dlog_print(DLOG_DEBUG, LOG_TAG, "media_tag_foreach_media_from_db() successful");
             int j = 0;
             media_info_h tag_media_handle;
             char *media_id = NULL;
             char *media_name = NULL;
             char *media_path = NULL;
             media_content_type_e media_type = MEDIA_CONTENT_TYPE_IMAGE;
    
             for (j = 0; j < g_list_length(media_list_in_tag); j++) 
             {
                tag_media_handle = (media_info_h)g_list_nth_data(media_list_in_tag, j);
                ret = media_info_get_media_id(tag_media_handle, &media_id);
    
                ret = media_info_get_display_name(tag_media_handle, &media_name);
    
                ret = media_info_get_file_path(tag_media_handle, &media_path);
    
                ret = media_info_get_media_type(tag_media_handle, &media_type);
    
                dlog_print(DLOG_DEBUG, LOG_TAG, "[%s] media_id [%d] : %s", tag_name, j, media_id);
                dlog_print(DLOG_DEBUG, LOG_TAG, "[%s] media_type [%d] : %d", tag_name, j, media_type);
                dlog_print(DLOG_DEBUG, LOG_TAG, "[%s] media_name [%d] : %s", tag_name, j, media_name);
                dlog_print(DLOG_DEBUG, LOG_TAG, "[%s] media_path [%d] : %s", tag_name, j, media_path);
    
                free(media_id);
                free(media_name);
                free(media_path);
                media_info_destroy(tag_media_handle);
             }
          }
          free(tag_name);
          media_tag_destroy(tag_handle);
          g_list_free(media_list_in_tag);
       }
       g_list_free(tag_list);
    }
    

Finding Media Item Groups

To find groups of media items and items in a given group:

  1. Search for groups.

    A group is a collection of media items which have the same value of a given property. For example, if the property is the artist, there are as many groups as there are artists, and each group consists of items by the same artist. The possible groups are determined by the media_group_e values, such as MEDIA_CONTENT_GROUP_ARTIST and MEDIA_CONTENT_GROUP_MIME_TYPE.

    1. Find the number of MIME type-related groups:

      media_group_e group = MEDIA_CONTENT_GROUP_MIME_TYPE;
      int count = -1; 
      
      media_group_get_group_count_from_db(NULL, group, &count);
      dlog_print(DLOG_DEBUG, LOG_TAG, "Group count: %d\n", count);
      

      The first parameter is the filter. In this example, the filter is NULL, which means that no filtering is performed and all items are considered when searching.

    2. Find groups using the media_group_foreach_group_from_db() function.

      The first parameter defines a filter, and the second the group identifier. The third parameter defines the callback called for each group, and the final parameter is the data passed to the callback upon each call.

      The following example searches all media items and provides all groups as the result. For each found group, the group_cb() function is called. The group identifier is given as the data passed to the callback.

      media_group_foreach_group_from_db(NULL, group, group_cb, (void *) group);
      
  2. Receive group data.
    1. Define the callback for receiving the group data. If the callback returns true, the iteration over the found groups continues. If the return value is false, the iteration is stopped.

      Each time the callback is called for a group, the group name is passed as the name parameter. In this case, names are set to various found MIME types, such as image/png and audio/mpeg.

      Having the group name and identifier (passed in user_data), find the number of items in the group using the media_group_get_media_count_from_db() function. The third parameter is the filter, set to NULL to count all items in the group.

      To find all items in the group, call the media_group_foreach_media_from_db() function. The parameters are similar to those in the media_group_foreach_media_from_db() function. The media_cb() callback is called for each found item.

      bool group_cb(const char *name, void *user_data)
      {
         media_content_error_e ret = MEDIA_CONTENT_ERROR_NONE;
         media_group_e group = (media_group_e) user_data;
      
         dlog_print(DLOG_DEBUG, LOG_TAG, "\n");
         dlog_print(DLOG_DEBUG, LOG_TAG, "Group name: %s\n", name);
      
         int count = -1; 
         media_group_get_media_count_from_db(name, group, NULL, &count);
         dlog_print(DLOG_DEBUG, LOG_TAG, "Media count in group: %d\n", count);
      
         media_group_foreach_media_from_db(name, group, NULL, media_cb, NULL);
      
         return true;
      }
      
    2. Define the media_cb() callback to handle each found item:
      bool media_cb(media_info_h media, void *user_data)
      {
         media_content_error_e ret = MEDIA_CONTENT_ERROR_NONE;
         char *name = NULL;
      
         media_info_get_display_name(media, &name);
         if (name != NULL) 
         {
            dlog_print(DLOG_DEBUG, LOG_TAG, "Name: %s\n", name);
            free(name);
         }
      
         return true;
      }
      

Finding Media Item Groups Using a Filter

To find groups containing only items matching the given criteria:

  1. Create a filter.

    When searching for groups, you can use a filter to limit what media items are considered. For example, search only for items containing a given substring in their name. Items which do not meet this condition are ignored, and groups are formed entirely of items which meet the condition. For example, if you group files by MIME type and only look for files containing '.jpg' in their name, groups which do not contain such files are not listed.

    filter_h filter;
    
    media_filter_create(&filter);
    
  2. Set the filter condition. The condition is a string used in a SQL query.

    The following example searches for files containing '.jpg' in their name. The '%' characters in the query act as wildcards. Also, they should be escaped using another '%' character to avoid compiler warnings.

    #define BUFLEN 200 // This is just an example
    char buf[BUFLEN] = {'\0'};
    
    snprintf(buf, BUFLEN, "%s LIKE '%%.jpg'", MEDIA_DISPLAY_NAME);
    media_filter_set_condition(filter, buf, MEDIA_CONTENT_COLLATE_DEFAULT);
    
  3. Search for groups as in the "Finding Media Item Groups" use case, but define the created filter as the first parameter. The same callbacks as before are used.

    int count = -1; 
    media_group_get_group_count_from_db(filter, group, &count);
    dlog_print(DLOG_DEBUG, LOG_TAG, "Group count: %d\n", count);
    
    media_group_foreach_group_from_db(filter, group, group_cb, (void *) group);
    
Go to top