Menampilkan karya seni media

Karya seni untuk item media harus diteruskan sebagai URI lokal menggunakan ContentResolver.SCHEME_CONTENT atau ContentResolver.SCHEME_ANDROID_RESOURCE. URI lokal ini harus me-resolve bitmap atau vektor drawable di resource aplikasi. Untuk objek MediaDescriptionCompat yang merepresentasikan item dalam hierarki konten, teruskan URI melalui setIconUri.

Untuk objek MediaMetadataCompat yang merepresentasikan item yang sedang diputar, gunakan salah satu kunci ini untuk meneruskan URI melalui putString:

Langkah-langkah ini menjelaskan cara mendownload art dari URI web dan menampilkannya melalui URI lokal. Untuk contoh lengkap, lihat implementasi openFile dan metode penyerta di aplikasi contoh Universal Android Music Player.

  1. Buat URI content:// yang sesuai dengan URI web. Layanan browser media dan sesi media meneruskan URI konten ini ke Android Auto dan 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();
     }
    
  2. Dalam implementasi ContentProvider.openFile, periksa apakah file ada untuk URI yang sesuai. Jika tidak, download dan cache file gambar. Cuplikan kode ini menggunakan 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);
      }
    

Untuk mengetahui detail tentang penyedia konten, lihat Membuat penyedia konten.