طرح هنری برای آیتمهای رسانهای باید به عنوان یک URI محلی با استفاده از ContentResolver.SCHEME_CONTENT یا ContentResolver.SCHEME_ANDROID_RESOURCE ارسال شود. این URI محلی باید به یک بیتمپ یا یک بردار قابل ترسیم تبدیل شود.
برای اشیاء
MediaDescriptionCompatکه نشاندهندهی آیتمها در سلسله مراتب محتوا هستند، URI را از طریقsetIconUriارسال کنید.برای اشیاء
MediaMetadataCompatکه نمایانگر آیتم در حال پخش هستند، از هر یک از این کلیدها برای ارسال URI از طریقputStringاستفاده کنید:
از منابع برنامه خود، آثار هنری ارائه دهید
برای ارائه drawableها از منابع برنامه خود، یک URI با فرمت زیر ارسال کنید:
android.resource://PACKAGE_NAME/RESOURCE_TYPE/RESOURCE_NAME
// Example URI - note that there is no file extension at the end of the URI
android.resource://com.example.app/drawable/example_drawable
این قطعه کد نحوه ایجاد یک URI با این فرمت را از یک شناسه منبع نشان میدهد:
val resources = context.resources
val resourceId: Int = R.drawable.example_drawable
Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(resources.getResourcePackageName(resourceId))
.appendPath(resources.getResourceTypeName(resourceId))
.appendPath(resources.getResourceEntryName(resourceId))
.build()
ارائه آثار هنری با استفاده از ارائه دهنده محتوا
این مراحل نحوه دانلود اثر هنری از یک URL وب و نمایش آن از طریق یک URI محلی با استفاده از یک ارائه دهنده محتوا را شرح میدهد. برای مثال کامل، پیادهسازی openFile و متدهای اطراف آن را در برنامه نمونه Universal Android Music Player ببینید.
یک URL
content://مطابق با URL وب بسازید. سرویس مرورگر رسانه و جلسه رسانه این URL محتوا را به Android Auto و AAOS منتقل میکنند.کاتلین
fun Uri.asAlbumArtContentURI(): Uri { return Uri.Builder() .scheme(ContentResolver.SCHEME_CONTENT) .authority(CONTENT_PROVIDER_AUTHORITY) .appendPath(this.getPath()) // Make sure you trust the URI .build() }جاوا
public static Uri asAlbumArtContentURI(Uri webUri) { return new Uri.Builder() .scheme(ContentResolver.SCHEME_CONTENT) .authority(CONTENT_PROVIDER_AUTHORITY) .appendPath(webUri.getPath()) // Make sure you trust the URI! .build(); }در پیادهسازی
ContentProvider.openFile، بررسی کنید که آیا فایلی برای URI مربوطه وجود دارد یا خیر. در غیر این صورت، فایل تصویر را دانلود و ذخیره کنید. این قطعه کد از Glide استفاده میکند.کاتلین
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? { val context = this.context ?: return null val file = File(context.cacheDir, uri.path) if (!file.exists()) { val remoteUri = Uri.Builder() .scheme("https") .authority("my-image-site") .appendPath(uri.path) .build() val cacheFile = Glide.with(context) .asFile() .load(remoteUri) .submit() .get(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS) cacheFile.renameTo(file) file = cacheFile } return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY) }جاوا
@Nullable @Override public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException { Context context = this.getContext(); File file = new File(context.getCacheDir(), uri.getPath()); if (!file.exists()) { Uri remoteUri = new Uri.Builder() .scheme("https") .authority("my-image-site") .appendPath(uri.getPath()) .build(); File cacheFile = Glide.with(context) .asFile() .load(remoteUri) .submit() .get(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS); cacheFile.renameTo(file); file = cacheFile; } return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); }