MediaDescription.Builder() not setting MediaItem Icon

372 Views Asked by At

This is a android automotive media application that uses the MediaBrowserService and i'm trying to set MediaItem MediaDescription Icon using a image in drawable folder.

This is the code I'm Using,

@Override
public void onLoadChildren(final String parentMediaId, final Result<List<MediaBrowser.MediaItem>> result) {
    // Return the children of the given parent media item
    List<MediaBrowser.MediaItem> children = getChildren(parentMediaId);
    result.sendResult(children);
}


private List<MediaBrowser.MediaItem> getChildren(String parentId) {
    int drawableResourceId = R.drawable.finallogo;
    Uri drawableUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
            + "com.examole.app" + "/" + drawableResourceId);
    List<MediaBrowser.MediaItem> children = new ArrayList<>();
    if (parentId.equals("root")) {

        children.add(new MediaBrowser.MediaItem(new MediaDescription.Builder()

                .setMediaId("media_item_1")
                .setTitle("Browsable Item")
                .setSubtitle("test")
                .setIconUri(drawableUri)
                .build(),MediaBrowser.MediaItem.FLAG_BROWSABLE ));
    }else{
        children.add(new MediaBrowser.MediaItem(new MediaDescription.Builder()
            .setMediaId("media_item_1")
            .setTitle("Playable Item")
            .setSubtitle("test2")
                .setIconUri(drawableUri)
            .build(), MediaBrowser.MediaItem.FLAG_PLAYABLE));
    }
    return children;
}

The documentation states,

Artwork for media items must be passed as a local URI using either ContentResolver.SCHEME_CONTENT or ContentResolver.SCHEME_ANDROID_RESOURCE. This local URI must resolve to either a bitmap or a vector drawable in the application’s resources. For MediaDescriptionCompat

and this code already uses ContentResolver,

 Uri drawableUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
            + "com.examole.app" + "/" + drawableResourceId);

Does the image needs to be cashed even before loading from local drawable folder? or can someone specify a way to set local drawable image to media item with onLoadChidren method. Thank you.

1

There are 1 best solutions below

0
On

If it is a file from drawable use this code.

metadata.putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, "android.resource://com.example.app/drawable/image");