Wyświetlanie elementów graficznych

Grafika elementów multimedialnych musi być przekazywana jako lokalny identyfikator URI za pomocą elementu ContentResolver.SCHEME_CONTENT lub ContentResolver.SCHEME_ANDROID_RESOURCE. Ten lokalny identyfikator URI musi prowadzić do mapy bitowej lub rysunku wektorowego w zasobach aplikacji. W przypadku MediaDescriptionCompatobiektów reprezentujących elementy w hierarchii treści przekaż identyfikator URI za pomocą parametru setIconUri.

W przypadku obiektów MediaMetadataCompat reprezentujących odtwarzany element użyj dowolnego z tych kluczy, aby przekazać identyfikator URI przez putString:

Poniżej znajdziesz instrukcje pobierania grafiki z identyfikatora URI w internecie i udostępniania jej za pomocą lokalnego identyfikatora URI. Pełny przykład znajdziesz w implementacji openFile i powiązanych metod w przykładowej aplikacji Universal Android Music Player.

  1. Utwórz content:// identyfikator URI odpowiadający identyfikatorowi URI w internecie. Usługa przeglądarki multimediów i sesja multimedialna przekazują ten identyfikator URI treści do Androida Auto i Androida 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. W implementacji ContentProvider.openFile sprawdź, czy istnieje plik odpowiadający danemu identyfikatorowi URI. Jeśli nie, pobierz plik obrazu i zapisz go w pamięci podręcznej. Ten fragment kodu korzysta z biblioteki 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);
      }
    

Więcej informacji o dostawcach treści znajdziesz w artykule Tworzenie dostawcy treści.