最近在做Android TV O的项目,需要在TV 桌面添加自定义频道/节目,节目的背景图片要显示为SD卡或者缓存目录里面的图片。
添加自定义频道
节目背景显示本地目录的图片
一、添加频道
1. 首先新建频道、节目实体类,属性如下。
public class MediaChannel {
private final String mName;
private final String mDescription;
private final String mMediaUri;
private final String mBgImage;
private final String mTitle;
private final String mMediaChannelId;
private List mPrograms;
private boolean mChannelPublished;
private long mChannelId;
MediaChannel(String name, List programs, String mediaChannelId) {
mName = name;
mTitle = "playlist title";
mDescription = "playlist description";
mMediaUri = "dsf";
mBgImage = "asdf";
mPrograms = programs;
mMediaChannelId = mediaChannelId;
}
// 省略 set get toString
}
public class MediaProgram implements Parcelable {
private final String mMediaProgramId;
private final String mContentId;
private final String mTitle;
private final String mDescription;
private final String mBgImageUrl;
private final String mCardImageUrl;
private final String mMediaUrl;
private final String mPreviewMediaUrl;
private final String mCategory;
private long mProgramId;
private int mViewCount;
MediaProgram(String title, String description, String bgImageUrl, String cardImageUrl,
String category, String mediaProgramId, String contentId) {
mMediaProgramId = mediaProgramId;
mContentId = contentId;
mTitle = title;
mDescription = description;
mBgImageUrl = bgImageUrl;
mCardImageUrl = cardImageUrl;
mMediaUrl = "";
mPreviewMediaUrl = "";
mCategory = category;
}
// 省略 set get toString
}
2. 初始化频道、节目信息
private void initChannel() {
Uri usbUri = getUSBCardImageFileUri();
Uri pvrUri = getPVRCardImageFileUri();
grantUriPermissionToApp("com.google.android.tvlauncher", usbUri);
grantUriPermissionToApp("com.google.android.tvlauncher", pvrUri);
String bgImageUrl = "";
String usbCardImageUrl = getUSBCardImageFileUri().toString();
String pvrCardImageUrl = getPVRCardImageFileUri().toString();
int mediaProgramId = 1;
int contentId = 0;
MediaProgram usbProgram = new MediaProgram("USB", "usb description", bgImageUrl, usbCardImageUrl,
"USB category", Integer.toString(mediaProgramId), Integer.toString(contentId ++));
MediaProgram pvrProgram = new MediaProgram("PVR", "pvr description", bgImageUrl, pvrCardImageUrl,
"PVR category", Integer.toString(mediaProgramId), Integer.toString(conten