Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
A arte em itens de mídia precisa ser transmitida como um URI local usando
ContentResolver.SCHEME_CONTENT ou
ContentResolver.SCHEME_ANDROID_RESOURCE. Esse URI local precisa ser resolvido para
um bitmap ou um drawable vetorial nos recursos do aplicativo. Para
objetos MediaDescriptionCompat que representam itens na hierarquia de conteúdo,
transmita o URI usando setIconUri.
Para objetos MediaMetadataCompat que representam o item em reprodução,
use uma destas chaves para transmitir o URI com putString:
Estas etapas descrevem como fazer o download da arte de um URI da Web e expô-la com um
URI local. Para conferir um exemplo completo, consulte a implementação de
openFile e os métodos próximos no app de exemplo Universal Android Music
Player.
Crie um URI content:// correspondente ao URI da Web. O serviço de navegador
de mídia e a sessão de mídia transmitem esse URI de conteúdo ao Android Auto e o
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();}
Na sua implementação de ContentProvider.openFile, verifique se existe um arquivo
para o URI correspondente. Caso contrário, faça o download e armazene o arquivo de imagem em cache. Este
snippet de código usa o Glide.
O conteúdo e os exemplos de código nesta página estão sujeitos às licenças descritas na Licença de conteúdo. Java e OpenJDK são marcas registradas da Oracle e/ou suas afiliadas.
Última atualização 2025-08-22 UTC.
[null,null,["Última atualização 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."]]