developer.android.com states that Android has native support for .avif since Android 12, but how do I display a .avif file in an ImageView for example or any UI component in Android? Do I have to rely on third-party libraries or plugins?
Best Regards.
I've tried to load the .avif file like any other image file into an ImageView. Android studio doesn't seem to recognize the file format, trying to access it results in app crashes or compiling errors.
Simply use coil if you are fine with using a dependency for that, they have it supported for both internet url along with local files uri. as of their list of supported formats.
add to build.gradle(:app)
implementation("io.coil-kt:coil:2.4.0")
then create an extension to Context using the following
suspend fun Context.loadImage(fileUri: Uri): ImageBitmap? = withContext(Dispatchers.IO) {
val imageLoader = ImageLoader.Builder(this@loadImage).build()
val request = ImageRequest.Builder(this@loadImage)
.data(fileUri)
.build()
val result = (imageLoader.execute(request) as? SuccessResult)?.drawable
return@withContext result?.toBitmap()?.asImageBitmap()
}
This code can load AVIF images as bitmaps, if you are using XML instead of Jetpack Compose, remove the ?.asImageBitmap()
of the return.
It is supported on Android 12+ (API 31+).
Sadly 'til this day there are is no code that I could rely on to save AVIF files on android.
Disclaimer: I myself have been looking to integrate it into my app since it has become supported everywhere including web.