ต้องส่งอาร์ตเวิร์กสำหรับรายการสื่อเป็น URI ในเครื่องโดยใช้
ContentResolver.SCHEME_CONTENT
หรือ
ContentResolver.SCHEME_ANDROID_RESOURCE
URI ในเครื่องนี้ต้องเปลี่ยนเส้นทางไปยัง
บิตแมปหรือ VectorDrawable
สำหรับออบเจ็กต์
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()
ส่งงานปกโดยใช้ผู้ให้บริการเนื้อหา
ขั้นตอนเหล่านี้อธิบายวิธีดาวน์โหลดอาร์ตเวิร์กจาก URI ของเว็บและแสดงผ่าน URI ในเครื่องโดยใช้ผู้ให้บริการเนื้อหา ดูตัวอย่างที่สมบูรณ์ได้ในการติดตั้งใช้งานของ openFile
และเมธอดโดยรอบใน
แอปตัวอย่าง Universal Android Music Player
สร้าง
content://
URI ที่สอดคล้องกับ URI ของเว็บ บริการเบราว์เซอร์สื่อและเซสชันสื่อจะส่ง URI ของเนื้อหานี้ไปยัง Android Auto และ AAOSKotlin
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 ที่เกี่ยวข้องหรือไม่ หากไม่มี ให้ดาวน์โหลดและแคชไฟล์รูปภาพ ข้อมูลโค้ดนี้ใช้ GlideKotlin
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); }