Grafika elementów multimedialnych musi być przekazywana jako lokalny identyfikator URI za pomocą
ContentResolver.SCHEME_CONTENT lub
ContentResolver.SCHEME_ANDROID_RESOURCE. Ten lokalny identyfikator URI musi wskazywać bitmapę lub obiekt rysowalny wektorowo.
W przypadku obiektów
MediaDescriptionCompatreprezentujących elementy w hierarchii treści przekaż identyfikator URI za pomocąsetIconUri.W przypadku obiektów
MediaMetadataCompatreprezentujących odtwarzany element, użyj dowolnego z tych kluczy, aby przekazać identyfikator URI za pomocąputString:
Podawanie grafiki z zasobów aplikacji
Aby podać obrazy drawable 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()
Podawanie grafiki za pomocą dostawcy treści
Z tych instrukcji dowiesz się, 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 otaczających ją metod w
przykładowej aplikacji Universal Android Music Player.
Utwórz identyfikator URI
content://odpowiadający identyfikatorowi URI w internecie. Usługa przeglądarki 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.openFilesprawdź, czy istnieje plik odpowiadający danemu identyfikatorowi URI. Jeśli nie, pobierz i zapisz w pamięci podręcznej plik obrazu. Ten fragment kodu używa 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); }