نمایش آثار هنری رسانه ای

آثار هنری برای موارد رسانه باید به عنوان یک URI محلی با استفاده از ContentResolver.SCHEME_CONTENT یا ContentResolver.SCHEME_ANDROID_RESOURCE ارسال شوند. این URI محلی باید به یک بیت مپ یا یک بردار قابل ترسیم در منابع برنامه حل شود. برای اشیاء MediaDescriptionCompat که نشان دهنده موارد در سلسله مراتب محتوا هستند، URI را از طریق setIconUri عبور دهید.

برای اشیاء MediaMetadataCompat که آیتم در حال پخش را نشان می‌دهند، از هر یک از این کلیدها برای عبور URI از putString استفاده کنید:

این مراحل نحوه بارگیری هنر از یک URI وب و نمایش آن از طریق یک URI محلی را توضیح می دهد. برای مثال کامل، اجرای openFile و متدهای اطراف آن را در برنامه نمونه برنامه پخش موسیقی جهانی اندروید ببینید.

  1. یک content:// URI مطابق با URI وب بسازید. سرویس مرورگر رسانه و جلسه رسانه این URI محتوا را به Android Auto و Android Automotive OS (AAOS) منتقل می‌کند.

    کاتلین

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

    جاوا

    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 استفاده می کند.

    کاتلین

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

    جاوا

    @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);
      }
    

برای جزئیات در مورد ارائه دهندگان محتوا، به ایجاد یک ارائه دهنده محتوا مراجعه کنید.

،

آثار هنری برای موارد رسانه باید به عنوان یک URI محلی با استفاده از ContentResolver.SCHEME_CONTENT یا ContentResolver.SCHEME_ANDROID_RESOURCE ارسال شوند. این URI محلی باید به یک بیت مپ یا یک بردار قابل ترسیم در منابع برنامه حل شود. برای اشیاء MediaDescriptionCompat که نشان دهنده موارد در سلسله مراتب محتوا هستند، URI را از طریق setIconUri عبور دهید.

برای اشیاء MediaMetadataCompat که آیتم در حال پخش را نشان می‌دهند، از هر یک از این کلیدها برای عبور URI از putString استفاده کنید:

این مراحل نحوه بارگیری هنر از یک URI وب و نمایش آن از طریق یک URI محلی را توضیح می دهد. برای مثال کامل، اجرای openFile و متدهای اطراف آن را در برنامه نمونه برنامه پخش موسیقی جهانی اندروید ببینید.

  1. یک content:// URI مطابق با URI وب بسازید. سرویس مرورگر رسانه و جلسه رسانه این URI محتوا را به Android Auto و Android Automotive OS (AAOS) منتقل می‌کند.

    کاتلین

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

    جاوا

    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 استفاده می کند.

    کاتلین

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

    جاوا

    @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);
      }
    

برای جزئیات در مورد ارائه دهندگان محتوا، به ایجاد یک ارائه دهنده محتوا مراجعه کنید.