Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hình minh hoạ cho các mục nội dung đa phương tiện phải được chuyển ở dạng URI cục bộ bằng ContentResolver.SCHEME_CONTENT hoặc ContentResolver.SCHEME_ANDROID_RESOURCE. URI cục bộ này phải phân giải đến tệp bitmap hoặc vectơ vẽ được trong tài nguyên của ứng dụng. Đối với các đối tượng MediaDescriptionCompat biểu thị các mục trong hệ thống phân cấp nội dung, hãy chuyển URI thông qua setIconUri.
Đối với đối tượng MediaMetadataCompat biểu thị mục đang phát, hãy dùng một trong những khoá sau đây để chuyển URI qua putString:
Các bước này mô tả cách tải hình minh hoạ xuống từ một URI web và hiển thị hình minh hoạ đó thông qua một URI cục bộ. Để biết ví dụ hoàn chỉnh, hãy xem cách triển khaiopenFile và các phương thức xung quanh trong ứng dụng mẫu Universal Android Music Player.
Tạo một URI content:// tương ứng với URI web. Dịch vụ trình duyệt nội dung đa phương tiện và phiên phát nội dung đa phương tiện sẽ chuyển URI nội dung này sang Android Auto và Android Automotive OS (AAOS).
Kotlin
funUri.asAlbumArtContentURI():Uri{returnUri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(CONTENT_PROVIDER_AUTHORITY).appendPath(this.getPath())// Make sure you trust the URI.build()}
Java
publicstaticUriasAlbumArtContentURI(UriwebUri){returnnewUri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(CONTENT_PROVIDER_AUTHORITY).appendPath(webUri.getPath())// Make sure you trust the URI!.build();}
Trong quá trình triển khai ContentProvider.openFile, hãy kiểm tra xem một tệp có tồn tại cho URI tương ứng hay không. Nếu không có, hãy tải xuống và lưu tệp hình ảnh vào bộ nhớ đệm. Đoạn mã này sử dụng Glide.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-22 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-22 UTC."],[],[],null,["# Display media artwork\n\nArtwork for media items must be passed as a local URI using either\n[`ContentResolver.SCHEME_CONTENT`](/reference/android/content/ContentResolver#SCHEME_CONTENT) or\n[`ContentResolver.SCHEME_ANDROID_RESOURCE`](/reference/android/content/ContentResolver#SCHEME_ANDROID_RESOURCE). This local URI must resolve to\neither a bitmap or a vector drawable.\n\n- For `MediaDescriptionCompat` objects representing items in the [content\n hierarchy](/training/cars/media/create-media-browser/content-hierarchy#onLoadChildren), pass the URI through [`setIconUri`](/reference/android/support/v4/media/MediaDescriptionCompat.Builder#setIconUri(android.net.Uri)).\n\n | **Warning:** Don't provide artwork using [`setIconBitmap`](/reference/android/support/v4/media/MediaDescriptionCompat.Builder#setIconUri(android.net.Uri)). While this method is supported on Android Auto, it isn't supported on Android Automotive OS (AAOS). Additionally, including many bitmaps in a result can cause you to exceed the [1MB binder size limit](/guide/components/activities/parcelables-and-bundles#sdbp), causing your app to be unresponsive.\n- For `MediaMetadataCompat` objects representing the [playing item](/media/legacy/mediasession#maintain-state),\n use any of these keys to pass the URI through [`putString`](/reference/android/support/v4/media/MediaMetadataCompat.Builder#putString(java.lang.String,%20java.lang.String)):\n\n - [`MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI`](/reference/android/support/v4/media/MediaMetadataCompat#METADATA_KEY_DISPLAY_ICON_URI())\n - [`MediaMetadataCompat.METADATA_KEY_ART_URI`](/reference/android/support/v4/media/MediaMetadataCompat#METADATA_KEY_ART_URI())\n - [`MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI`](/reference/android/support/v4/media/MediaMetadataCompat#METADATA_KEY_ALBUM_ART_URI())\n\nProvide artwork from your app's resources\n-----------------------------------------\n\nTo provide drawables from your [app's resources](/guide/topics/resources/providing-resources), pass a URI in the\nfollowing format: \n\n android.resource://\u003cvar translate=\"no\"\u003ePACKAGE_NAME\u003c/var\u003e/\u003cvar translate=\"no\"\u003eRESOURCE_TYPE\u003c/var\u003e/\u003cvar translate=\"no\"\u003eRESOURCE_NAME\u003c/var\u003e\n\n // Example URI - note that there is no file extension at the end of the URI\n android.resource://com.example.app/drawable/example_drawable\n\nThis snippet demonstrates how to create a URI of this format from a resource ID: \n\n val resources = context.resources\n val resourceId: Int = R.drawable.example_drawable\n\n Uri.Builder()\n .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)\n .authority(resources.getResourcePackageName(resourceId))\n .appendPath(resources.getResourceTypeName(resourceId))\n .appendPath(resources.getResourceEntryName(resourceId))\n .build()\n\nProvide artwork using a content provider\n----------------------------------------\n\nThese steps describe how to download art from a web URI and expose it through a\nlocal URI using a [content provider](/guide/topics/providers/content-provider-creating). For a complete example, see the\n[implementation](https://github.com/android/uamp/blob/99e44c1c5106218c62eff552b64bbc12f1883a22/common/src/main/java/com/example/android/uamp/media/library/AlbumArtContentProvider.kt#L52) of [`openFile`](/reference/android/content/ContentProvider#openFile(android.net.Uri,%20java.lang.String)) and the surrounding methods in the\nUniversal Android Music Player sample app.\n\n1. Build a `content://` URI corresponding to the web URI. The media browser\n service and media session pass this content URI to Android Auto and\n AAOS.\n\n ### Kotlin\n\n fun Uri.asAlbumArtContentURI(): Uri {\n return Uri.Builder()\n .scheme(ContentResolver.SCHEME_CONTENT)\n .authority(CONTENT_PROVIDER_AUTHORITY)\n .appendPath(this.getPath()) // Make sure you trust the URI\n .build()\n }\n\n ### Java\n\n public static Uri asAlbumArtContentURI(Uri webUri) {\n return new Uri.Builder()\n .scheme(ContentResolver.SCHEME_CONTENT)\n .authority(CONTENT_PROVIDER_AUTHORITY)\n .appendPath(webUri.getPath()) // Make sure you trust the URI!\n .build();\n }\n\n2. In your implementation of `ContentProvider.openFile`, check if a file exists\n for the corresponding URI. If not, download and cache the image file. This\n code snippet uses [Glide](https://github.com/bumptech/glide).\n\n ### Kotlin\n\n override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {\n val context = this.context ?: return null\n val file = File(context.cacheDir, uri.path)\n if (!file.exists()) {\n val remoteUri = Uri.Builder()\n .scheme(\"https\")\n .authority(\"my-image-site\")\n .appendPath(uri.path)\n .build()\n val cacheFile = Glide.with(context)\n .asFile()\n .load(remoteUri)\n .submit()\n .get(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)\n\n cacheFile.renameTo(file)\n file = cacheFile\n }\n return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)\n }\n\n ### Java\n\n @Nullable\n @Override\n public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode)\n throws FileNotFoundException {\n Context context = this.getContext();\n File file = new File(context.getCacheDir(), uri.getPath());\n if (!file.exists()) {\n Uri remoteUri = new Uri.Builder()\n .scheme(\"https\")\n .authority(\"my-image-site\")\n .appendPath(uri.getPath())\n .build();\n File cacheFile = Glide.with(context)\n .asFile()\n .load(remoteUri)\n .submit()\n .get(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS);\n\n cacheFile.renameTo(file);\n file = cacheFile;\n }\n return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);\n }\n\n| **Note:** The content URI should be quickly constructed and sent to Android Auto and AAOS, as demonstrated in the previous example. This is true even when the file isn't downloaded. Android Auto and AAOS show a loading UI for the images when waiting for the content provider to respond. Consider optimizing your app to quickly fetch images and to minimize the time needed to load the UI. Consider preloading and caching images."]]