แสดงอาร์ตเวิร์กสื่อ

ต้องส่งอาร์ตเวิร์กสำหรับรายการสื่อเป็น URI ในเครื่องโดยใช้ ContentResolver.SCHEME_CONTENT หรือ ContentResolver.SCHEME_ANDROID_RESOURCE URI ในเครื่องนี้ต้องเปลี่ยนเส้นทางไปยังบิตแมปหรือเวกเตอร์ที่วาดได้ในทรัพยากรของแอปพลิเคชัน สำหรับ MediaDescriptionCompat ออบเจ็กต์ที่แสดงถึงรายการในลำดับชั้นของเนื้อหา ให้ส่ง URI ผ่าน setIconUri

สำหรับออบเจ็กต์ MediaMetadataCompat ที่แสดงรายการที่กำลังเล่น ให้ใช้คีย์ใดก็ได้ต่อไปนี้เพื่อส่ง URI ผ่าน putString

ขั้นตอนเหล่านี้อธิบายวิธีดาวน์โหลดอาร์ตเวิร์กจาก 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);
      }
    

ดูรายละเอียดเกี่ยวกับผู้ให้บริการเนื้อหาได้ที่สร้างผู้ให้บริการเนื้อหา