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 di-resolve ke bitmap atau vektor drawable.

Menyediakan gambar dari resource aplikasi Anda

Untuk menyediakan drawable dari resource aplikasi, teruskan URI dalam format berikut:

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

Cuplikan ini menunjukkan cara membuat URI format ini dari ID resource:

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()

Menyediakan gambar menggunakan penyedia konten

Langkah-langkah ini menjelaskan cara mendownload art dari URI web dan menampilkannya melalui URI lokal menggunakan penyedia konten. 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 harus meneruskan URI konten ini ke Android Auto dan 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);
      }