0% found this document useful (0 votes)
8 views

Loading Images - Jetpack Compose - Android Developers

Uploaded by

Hien Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Loading Images - Jetpack Compose - Android Developers

Uploaded by

Hien Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/9/24, 10:34 AM Loading images | Jetpack Compose | Android Developers

Loading images
Load an image from the disk
Use the Image (/reference/kotlin/androidx/compose/foundation/package-summary#Image)
composable to display a graphic on screen. To load an image (for example: PNG, JPEG,
WEBP) or vector resource from the disk, use the painterResource
(/reference/kotlin/androidx/compose/ui/res/package-summary#painterresource) API with your
image reference. You don't need to know the type of the asset, just use
painterResource in Image or paint modifiers.

DrawScope :

Image(
painter = painterResource(id = R.drawable.dog),
contentDescription = stringResource(id = R.string.dog_content_description
)
/snippets/src/main/java/com/example/compose/snippets/images/LoadingImagesSnippets.kt#L47-L50)

To ensure that your app is accessible (/develop/ui/compose/accessibility), supply a


contentDescription for visual elements on screen. TalkBack reads out the content
description, so you must ensure that the text is meaningful if read out loud and
translated. In the above example, a stringResource() is used to load up the translated
content description from the strings.xml file. If your visual element on screen is purely
for visual decoration, set your contentDescription to null for the screen reader to
ignore it.

If you need lower-level ImageBitmap specific functionality, you can use


ImageBitmap.imageResource() to load up a Bitmap. For more information on
ImageBitmaps, read the ImageBitmap versus ImageVector
(/develop/ui/compose/graphics/images/compare) section.

Drawable support
painterResource currently supports the following drawable types:

AnimatedVectorDrawable (/reference/android/graphics/drawable/AnimatedVectorDrawable)

https://developer.android.com/develop/ui/compose/graphics/images/loading 1/3
8/9/24, 10:34 AM Loading images | Jetpack Compose | Android Developers

BitmapDrawable (/reference/android/graphics/drawable/BitmapDrawable) (PNG, JPG,


WEBP)

ColorDrawable (/reference/android/graphics/drawable/ColorDrawable)

VectorDrawable (/reference/android/graphics/drawable/VectorDrawable)

Load an image from the internet


To load an image from the internet, there are several third-party libraries available to help
you handle the process. Image loading libraries do a lot of the heavy lifting for you; they
handle both caching (so you don't download the image multiple times) and networking
logic to download the image and display it on screen.

For example, to load an image with Coil (https://github.com/coil-kt/coil#jetpack-compose) from


Instacart, add the library to your gradle file, and use an AsyncImage to load an image
from a URL:

AsyncImage(
model = "https://example.com/image.jpg",
contentDescription = "Translated description of what the image contains"
)
/snippets/src/main/java/com/example/compose/snippets/images/LoadingImagesSnippets.kt#L58-L61)

Coil Glide
(https://github.com/coil-kt/coil#jetpack- (https://bumptech.github.io/glide/int/compose.h
compose) tml)

An image loading library backed by Kotlin A fast and efficient image loading library
Coroutines (Instacart). for Android focused on smooth scrolling
maven-central v2.7.0
(Google).
(https://search.maven.org/artifact/io.coil-kt/coil- maven-central v1.0.0-beta01
compose) (https://search.maven.org/artifact/com.github.bu
mptech.glide/compose)

https://developer.android.com/develop/ui/compose/graphics/images/loading 2/3
8/9/24, 10:34 AM Loading images | Jetpack Compose | Android Developers

Previous
arrow_back Overview (/develop/ui/compose/graphics/images)
Next
ImageBitmap vs ImageVector (/develop/ui/compose/graphics/images/compare) arrow_forward

Content and code samples on this page are subject to the licenses described in the Content License
(/license). Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2024-08-06 UTC.

https://developer.android.com/develop/ui/compose/graphics/images/loading 3/3

You might also like