Grafika elementów multimedialnych musi być przekazywana jako lokalny identyfikator URI za pomocą elementu ContentResolver.SCHEME_CONTENT
lub ContentResolver.SCHEME_ANDROID_RESOURCE
. Ten lokalny identyfikator URI musi prowadzić do bitmapy lub rysunku wektorowego.
W przypadku obiektów
MediaDescriptionCompat
reprezentujących elementy w hierarchii treści przekaż identyfikator URI za pomocą parametrusetIconUri
.W przypadku obiektów
MediaMetadataCompat
reprezentujących odtwarzany element użyj dowolnego z tych kluczy, aby przekazać identyfikator URI przezputString
:
Prześlij grafikę z zasobów aplikacji
Aby udostępnić elementy rysowalne z zasobów aplikacji, przekaż identyfikator URI w tym formacie:
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
Ten fragment kodu pokazuje, jak utworzyć identyfikator URI w tym formacie na podstawie identyfikatora zasobu:
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()
Przesyłanie grafiki za pomocą dostawcy treści
Poniższe czynności opisują, jak pobrać grafikę z identyfikatora URI w internecie i udostępnić ją za pomocą lokalnego identyfikatora URI przy użyciu dostawcy treści. Pełny przykład znajdziesz w implementacji openFile
i powiązanych metod w przykładowej aplikacji Universal Android Music Player.
Utwórz
content://
identyfikator URI odpowiadający identyfikatorowi URI w internecie. Przeglądarka multimediów i sesja multimediów przekazują ten identyfikator URI treści do Androida Auto i 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(); }
W implementacji
ContentProvider.openFile
sprawdź, czy istnieje plik odpowiadający danemu identyfikatorowi URI. Jeśli nie, pobierz plik obrazu i zapisz go w pamięci podręcznej. Ten fragment kodu korzysta z biblioteki 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); }