मीडिया आइटम के लिए आर्टवर्क को लोकल यूआरआई के तौर पर पास किया जाना चाहिए. इसके लिए, ContentResolver.SCHEME_CONTENT
या ContentResolver.SCHEME_ANDROID_RESOURCE
में से किसी एक का इस्तेमाल करें. यह लोकल यूआरआई, बिटमैप या वेक्टर ड्रॉएबल में से किसी एक पर ले जाना चाहिए.
कॉन्टेंट के क्रम में मौजूद आइटम के बारे में बताने वाले
MediaDescriptionCompat
ऑब्जेक्ट के लिए,setIconUri
के ज़रिए यूआरआई पास करें.चल रहे आइटम को दिखाने वाले
MediaMetadataCompat
ऑब्जेक्ट के लिए,putString
के ज़रिए यूआरआई पास करने के लिए, इनमें से किसी भी कुंजी का इस्तेमाल करें:
अपने ऐप्लिकेशन के संसाधनों से आर्टवर्क उपलब्ध कराएं
अपने ऐप्लिकेशन के संसाधनों से ड्रॉएबल उपलब्ध कराने के लिए, इस फ़ॉर्मैट में यूआरआई पास करें:
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
इस स्निपेट में, रिसॉर्स आईडी से इस फ़ॉर्मैट का यूआरआई बनाने का तरीका बताया गया है:
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()
कॉन्टेंट उपलब्ध कराने वाली कंपनी का इस्तेमाल करके, आर्टवर्क उपलब्ध कराना
इन चरणों में, वेब यूआरआई से आर्ट डाउनलोड करने और कॉन्टेंट प्रोवाइडर का इस्तेमाल करके, उसे लोकल यूआरआई के ज़रिए दिखाने का तरीका बताया गया है. पूरे उदाहरण के लिए, Universal Android Music Player के सैंपल ऐप्लिकेशन में openFile
और इससे जुड़े तरीकों का इस्तेमाल देखें.
वेब यूआरआई से मिलता-जुलता
content://
यूआरआई बनाएं. मीडिया ब्राउज़र सेवा और मीडिया सेशन, इस कॉन्टेंट यूआरआई को Android Auto और 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(); }
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); }