Skip to main content
Platform
Camera Kit Android

Prefetch Lenses

Overview

To present a Lens experience as soon as the user opens up the camera screen, you may want to download the Lens beforehand. This way users don't have to wait for the Lens to be downloaded when they open the camera screen. This also helps when there is poor or no internet connectivity. This is the solution we recommend for those looking to support Lenses in offline mode, instead of bundling Lenses with your application. Lenses can still be experienced from the device's cache memory. It's recommended to prefetch Lenses as soon as the user lands on a page from where they can launch the camera screen. Below are the steps to prefetch Lenses.

Please make sure app has enough cache memory to cache all the Lenses you are prefetching and planning on using when device is offline. Typically you should have 4-8MB/Lens for every cached Lens to avoid cache evictions. App can configure cache using configureCache API on Android.

Android Implementation

Here is a code block that demonstrates how the Prefetcher exposed from the LensesComponent can be used to prefetch Lenses. You can try this example in camerakit-sample-full sample app.

// Make sure to call `close` on `lensesPrefetch` once done with the Camera Kit SDK to avoid memory leaks
var lensesPrefetch = Closeable {}
findViewById<Button>(R.id.lenses_prefetch_button).setOnClickListener {
session.lenses.repository.get(Available(*lensGroups)) { available ->
available.whenHasSome { lenses ->
// Cancel any running prefetch operation before submitting new one
lensesPrefetch.close()
// Prefetch available lenses content async
lensesPrefetch = session.lenses.prefetcher.run(lenses) { success ->
Log.d(TAG, "Finished prefetch of [${lenses.size}] lenses with success: $success"
}
}
}
}

If you are using CameraActivity then you can prefetch the Lens(es) using their ID as regex pattern. You can try this example in camerakit-sample-simple sample app.

CameraActivity.Configuration.WithLenses(
lensGroupIds = LENS_GROUP_IDS,
prefetchLensByIdPattern = "\\S+"
)
Was this page helpful?
Yes
No