Get all the folders that contain media files

This set of functions will help you to get all the folders that contain media files. Empty folders are not listed.
#include <dlog.h> //for logging purposes
#include <media_content.h>
#include <stdlib.h>

//Callback to be executed on each folder found containing multimedia
bool folder_callback(media_folder_h folder, void *user_data)
{
    // Here you can do anything with the folder found.
	// In this snippet we get the various properties and print them to logs.
    
	char *id = NULL;
	char *name = NULL;
	char *path = NULL;
	int media_count = 0;

	if ((media_folder_get_folder_id(folder, &id) == MEDIA_CONTENT_ERROR_NONE)
		&& (media_folder_get_name(folder, &name) == MEDIA_CONTENT_ERROR_NONE)
		&& (media_folder_get_path(folder, &path) == MEDIA_CONTENT_ERROR_NONE)
		&& (media_folder_get_media_count_from_db(id, NULL, &media_count) == MEDIA_CONTENT_ERROR_NONE))
	{
		dlog_print(DLOG_INFO, LOG_TAG, "Media folder found!");
		dlog_print(DLOG_INFO, LOG_TAG, "Name: %s", name);
		dlog_print(DLOG_INFO, LOG_TAG, "Path: %s", path);
		dlog_print(DLOG_INFO, LOG_TAG, "Number of media items: %d", media_count);
	}
	else
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get folder properties!");
	}

	free (id);
	free (name);
	free (path);

	return true;
}

bool list_media_folders()
{
	if (media_content_connect() == MEDIA_CONTENT_ERROR_NONE)
	{
		if (media_folder_foreach_folder_from_db(NULL, folder_callback, NULL) != MEDIA_CONTENT_ERROR_NONE)
		{
		   dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when trying to list folders!");
		   media_content_disconnect();
		   return false;
		}
		if (media_content_disconnect() != MEDIA_CONTENT_ERROR_NONE)
		{
		   dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when disconnecting from media db!");
		   return false;
		}
		return true;
	}
	else
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when connecting to media db!");
		return false;
	}
}

Responses

0 Replies