Loading the AVIF file
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+).
Saving the AVIF file
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.