הצגת גרפיקה של מדיה

צריך להעביר את יצירות האומנות של פריטי המדיה כמזהה משאבים אחיד (URI) מקומי באמצעות ContentResolver.SCHEME_CONTENT או ContentResolver.SCHEME_ANDROID_RESOURCE. ה-URI המקומי הזה צריך להפנות ל-bitmap או ל-vector drawable במשאבים של האפליקציה. לאובייקטים MediaDescriptionCompat שמייצגים פריטים בהיררכיית התוכן, מעבירים את ה-URI באמצעות setIconUri.

כדי להעביר את ה-URI דרך putString, משתמשים באחד מהמפתחות האלה לאובייקטים של MediaMetadataCompat שמייצגים את הפריט שמופעל:

בשלבים הבאים מוסבר איך להוריד יצירת אמנות מ-URI באינטרנט ולחשוף אותה באמצעות URI מקומי. דוגמה מלאה אפשר לראות בהטמעה של openFile והשיטות שמסביב באפליקציית הדוגמה Universal Android Music Player.

  1. יוצרים content:// URI שמתאים ל-URI של האינטרנט. שירות דפדפן המדיה והסשן של המדיה מעבירים את ה-URI של התוכן הזה אל Android Auto ואל 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. בהטמעה של ContentProvider.openFile, בודקים אם קיים קובץ עבור ה-URI המתאים. אם לא, מורידים את קובץ התמונה ושומרים אותו במטמון. קטע הקוד הזה משתמש ב-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);
      }
    

פרטים על ספקי תוכן זמינים במאמר בנושא יצירת ספק תוכן.