عرض الأعمال الفنية من الوسائط

يجب تمرير العمل الفني الخاص بعناصر الوسائط كمعرّف موارد منتظم (URI) محلي باستخدام ContentResolver.SCHEME_CONTENT أو ContentResolver.SCHEME_ANDROID_RESOURCE. يجب أن يؤدي عنوان URI المحلي هذا إلى عرض إما صورة نقطية أو رسم متّجه.

توفير عمل فني من موارد تطبيقك

لتوفير عناصر قابلة للرسم من موارد تطبيقك، مرِّر معرّف URI بالتنسيق التالي:

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

يوضّح هذا المقتطف كيفية إنشاء معرّف موارد موحّد (URI) بهذا التنسيق من معرّف مورد:

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

توفير العمل الفني باستخدام موفّر محتوى

توضّح هذه الخطوات كيفية تنزيل عمل فني من معرّف موارد منتظم (URI) على الويب وعرضه من خلال معرّف موارد منتظم (URI) محلي باستخدام موفّر محتوى. للاطّلاع على مثال كامل، راجِع عملية التنفيذ الخاصة بالرمز openFile والطُرق المحيطة به في تطبيق Universal Android Music Player النموذجي.

  1. أنشئ معرّف موارد منتظم (URI) content:// مطابقًا لمعرّف الموارد المنتظم (URI) على الويب. وتمرِّر خدمة متصفّح الوسائط وجلسة الوسائط معرّف الموارد المنتظم (URI) الخاص بهذا المحتوى إلى Android Auto وAndroid Automotive OS.

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