Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Das Artwork für Media-Elemente muss als lokaler URI über ContentResolver.SCHEME_CONTENT oder ContentResolver.SCHEME_ANDROID_RESOURCE übergeben werden. Dieser lokale URI muss entweder in einer Bitmap oder einem Vektordrawable in den Ressourcen der Anwendung aufgelöst werden. Für MediaDescriptionCompat-Objekte, die Elemente in der Inhaltshierarchie darstellen, übergeben Sie den URI über setIconUri.
Verwenden Sie für MediaMetadataCompat-Objekte, die das wiedergegebene Element darstellen, einen dieser Schlüssel, um den URI über putString zu übergeben:
In diesen Schritten wird beschrieben, wie Sie Grafiken von einem Web-URI herunterladen und über einen lokalen URI verfügbar machen. Ein vollständiges Beispiel finden Sie in der Implementierung von openFile und den umgebenden Methoden in der Universal Android Music Player-Beispiel-App.
Erstellen Sie einen content://-URI, der dem Web-URI entspricht. Der Media Browser-Dienst und die Media Session übergeben diesen Inhalts-URI an Android Auto und 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();}
Prüfen Sie in Ihrer Implementierung von ContentProvider.openFile, ob eine Datei für den entsprechenden URI vorhanden ist. Falls nicht, laden Sie die Bilddatei herunter und speichern Sie sie im Cache. In diesem Code-Snippet wird Glide verwendet.
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-08-22 (UTC).
[null,null,["Zuletzt aktualisiert: 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."]]