मीडिया आर्टवर्क दिखाएं

मीडिया आइटम के लिए आर्टवर्क को लोकल यूआरआई के तौर पर पास किया जाना चाहिए. इसके लिए, ContentResolver.SCHEME_CONTENT या ContentResolver.SCHEME_ANDROID_RESOURCE में से किसी एक का इस्तेमाल करें. यह लोकल यूआरआई, ऐप्लिकेशन के संसाधनों में मौजूद बिटमैप या वेक्टर ड्रॉएबल में से किसी एक पर रीडायरेक्ट होना चाहिए. कॉन्टेंट के क्रम में मौजूद आइटम दिखाने वाले MediaDescriptionCompat ऑब्जेक्ट के लिए, setIconUri के ज़रिए यूआरआई पास करें.

प्ले किए जा रहे आइटम को दिखाने वाले MediaMetadataCompat ऑब्जेक्ट के लिए, putString के ज़रिए यूआरआई पास करने के लिए, इनमें से किसी भी कुंजी का इस्तेमाल करें:

इस तरीके में, वेब यूआरआई से आर्टवर्क डाउनलोड करने और उसे लोकल यूआरआई के ज़रिए दिखाने का तरीका बताया गया है. पूरे उदाहरण के लिए, Universal Android Music Player के सैंपल ऐप्लिकेशन में openFile और इससे जुड़े तरीकों को लागू करने का तरीका देखें.

  1. वेब यूआरआई से मिलता-जुलता content:// यूआरआई बनाएं. मीडिया ब्राउज़र सेवा और मीडिया सेशन, इस कॉन्टेंट यूआरआई को 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 को लागू करते समय, देखें कि क्या उससे जुड़े यूआरआई के लिए कोई फ़ाइल मौजूद है. अगर ऐसा नहीं है, तो इमेज फ़ाइल को डाउनलोड करके कैश मेमोरी में सेव करें. इस कोड स्निपेट में 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);
      }
    

कॉन्टेंट उपलब्ध कराने वाली कंपनियों के बारे में जानकारी के लिए, कॉन्टेंट उपलब्ध कराने वाली कंपनी बनाना लेख पढ़ें.