צריך להעביר את יצירות האומנות של פריטי המדיה כמזהה משאבים אחיד (URI) מקומי באמצעות ContentResolver.SCHEME_CONTENT
או ContentResolver.SCHEME_ANDROID_RESOURCE
. ה-URI המקומי הזה חייב להפנות ל-bitmap או ל-vector drawable.
לאובייקטים מסוג
MediaDescriptionCompat
שמייצגים פריטים בהיררכיית התוכן, מעבירים את ה-URI דרךsetIconUri
.כדי להעביר את ה-URI דרך
putString
, אפשר להשתמש בכל אחד מהמפתחות האלה עבור אובייקטים שלMediaMetadataCompat
שמייצגים את הפריט שמופעל:
שליחת יצירות אומנות מהמשאבים של האפליקציה
כדי לספק משאבים מסוג drawable מהמשאבים של האפליקציה, מעבירים 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.
יוצרים
content://
URI שמתאים ל-URI של האינטרנט. שירות דפדפן המדיה ומעבר סשן המדיה של URI התוכן הזה אל 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
, בודקים אם קיים קובץ עבור ה-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); }