يجب تمرير العمل الفني الخاص بعناصر الوسائط كمعرّف موارد منتظم (URI) محلي باستخدام ContentResolver.SCHEME_CONTENT
أو ContentResolver.SCHEME_ANDROID_RESOURCE
. يجب أن يؤدي معرّف الموارد المنتظم (URI) المحلي هذا إلى عرض إما صورة نقطية أو رسم متّجه في موارد التطبيق. بالنسبة إلى عناصر
MediaDescriptionCompat
التي تمثّل عناصر في التسلسل الهرمي للمحتوى،
مرِّر معرّف الموارد المنتظم من خلال setIconUri
.
بالنسبة إلى عناصر MediaMetadataCompat
التي تمثّل العنصر الذي يتم تشغيله،
استخدِم أيًا من المفاتيح التالية لتمرير معرّف الموارد المنتظم (URI) من خلال putString
:
MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI
MediaMetadataCompat.METADATA_KEY_ART_URI
MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI
توضّح هذه الخطوات كيفية تنزيل عمل فني من معرّف موارد موحّد (URI) على الويب وعرضه من خلال معرّف موارد موحّد (URI) محلي. للحصول على مثال كامل، راجِع عملية التنفيذ الخاصة بالرمز openFile
والطُرق المحيطة به في نموذج تطبيق Universal Android Music Player.
أنشئ معرّف موارد منتظم (URI)
content://
مطابقًا لمعرّف الموارد المنتظم (URI) على الويب. تمرِّر خدمة متصفّح الوسائط وجلسة الوسائط معرّف الموارد المنتظم (URI) الخاص بهذا المحتوى إلى Android Auto وAndroid Automotive OS (AAOS).Kotlin
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() }
Java
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.Kotlin
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) }
Java
@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); }
للحصول على تفاصيل حول موفّري المحتوى، يُرجى الاطّلاع على إنشاء موفّر محتوى.