Help on Error: Futures.immediateFuture can only be called from within the same library group (groupid=androidx.camera)

604 views
Skip to first unread message

Stuart Schechter

unread,
Feb 3, 2020, 8:30:49 PM2/3/20
to Android CameraX Discussion Group
I'm getting errors when I use sample code from

import androidx.appcompat.app.AppCompatActivity

class X: AppCompatActivity(), CameraXConfig.Provider {
   ...

   fun everythingInThisFunctionIsOfficalSampleCodeItShouldLikeTotallyWork(cameraProvider : ProcessCameraProvider) {
var cameraSelector : CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()

val preview = Preview.Builder().build()

preview.setPreviewSurfaceProvider {
resolution, surfaceReleaseFuture ->
// Create the SurfaceTexture with the required resolution
val surfaceTexture = android.graphics.SurfaceTexture(0)
surfaceTexture.setDefaultBufferSize(resolution.width, resolution.height)
val surface = Surface(surfaceTexture)

// Once surfaceReleaseFuture completes, the Surface and SurfaceTexture
// are no longer used by the camera hence safe to close.
surfaceReleaseFuture.addListener(
Runnable {
surface.release()
surfaceTexture.release()
}, ContextCompat.getMainExecutor(this))

// Return the Surface back in a ListenableFuture
Futures.
immediateFuture(surface)
}

cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}
  ...
}

ERROR
Futures.immediateFuture can only be called from within the same library group (groupid=androidx.camera)

The Futures class was imported, by Android Studio's suggestions, from
   import androidx.camera.core.impl.utils.futures.Futures

As for the versions...
    def camerax_version = "1.0.0-alpha09"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    // If you want to use the CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha06"
    // If you want to use the CameraX Extensions library
    implementation "androidx.camera:camera-extensions:1.0.0-alpha06"
    // If you want to use the CameraX Lifecycle library
    implementation "androidx.camera:camera-lifecycle:1.0.0-alpha03"

Thanks for the help

Stuart

ps. It would sure be nice if the published example code had fully qualified names.  All sorts of mistakes seem to result when they don't.

Xi Zhang

unread,
Feb 4, 2020, 1:40:33 PM2/4/20
to Stuart Schechter, Android CameraX Discussion Group
Thank you for reporting this issue. Our code example is actually not correct. CallbackToFutureAdapter.getFuture() should be used to return the Future. Here is a correct example:

preview.setPreviewSurfaceProvider {
resolution, surfaceReleaseFuture ->
// Create the SurfaceTexture with the required resolution
val surfaceTexture = android.graphics.SurfaceTexture(0)
surfaceTexture.setDefaultBufferSize(resolution.width, resolution.height)
val surface = Surface(surfaceTexture)

    // are no longer used by the camera hence safe to close.
surfaceReleaseFuture.addListener(
Runnable {
surface.release()
surfaceTexture.release()
}, ContextCompat.getMainExecutor(this)
)

    CallbackToFutureAdapter.getFuture {
completer ->
completer.set(surface);
"provide preview surface";
}
}

Alternatively, you can use PreviewView which takes care of this for you. The low-level setPreviewSurfaceProvider() can be tricky. If you prefer to use it,  please checkout the code in TextureView Implementation as an example.

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/e5834664-af48-435c-a367-3eb1d6b5204f%40android.com.

Stuart Schechter

unread,
Feb 4, 2020, 6:00:53 PM2/4/20
to Android CameraX Discussion Group, stuart.s...@gmail.com
Thanks!

For anyone else reading this thread,
  CallbackToFutureAdapter => androidx.concurrent.futures.CallbackToFutureAdapter 
 which requires dependencies you can find at:
dependencies {
    implementation
"androidx.concurrent:concurrent-futures:1.0.0"

   
// Kotlin
    implementation
"androidx.concurrent:concurrent-futures-ktx:1.1.0-alpha01"
}
(I think if you're using Kotlin you should have both lines, but I'm not sure.  Hopefully future documentation helps.)

On Wednesday, February 5, 2020 at 3:40:33 AM UTC+9, Xi Zhang wrote:
Thank you for reporting this issue. Our code example is actually not correct. CallbackToFutureAdapter.getFuture() should be used to return the Future. Here is a correct example:

preview.setPreviewSurfaceProvider {
resolution, surfaceReleaseFuture ->
// Create the SurfaceTexture with the required resolution
val surfaceTexture = android.graphics.SurfaceTexture(0)
surfaceTexture.setDefaultBufferSize(resolution.width, resolution.height)
val surface = Surface(surfaceTexture)

// are no longer used by the camera hence safe to close.
surfaceReleaseFuture.addListener(
Runnable {
surface.release()
surfaceTexture.release()
}, ContextCompat.getMainExecutor(this)
)

CallbackToFutureAdapter.getFuture {
completer ->
completer.set(surface);
"provide preview surface";
}
}

Alternatively, you can use PreviewView which takes care of this for you. The low-level setPreviewSurfaceProvider() can be tricky. If you prefer to use it,  please checkout the code in TextureView Implementation as an example.

To unsubscribe from this group and stop receiving emails from it, send an email to camerax-developers+unsub...@android.com.

Oscar Wahltinez‎

unread,
Feb 5, 2020, 8:50:52 PM2/5/20
to Android CameraX Discussion Group
Thanks for flagging this. We are working on updating the docs. Xi's example is specific to TextureView, please see https://issuetracker.google.com/issues/146957342#comment6 for a generic Surface example and to follow the discussion regarding documentation updates.

Stuart Schechter

unread,
Feb 5, 2020, 11:23:40 PM2/5/20
to Android CameraX Discussion Group
Thanks to Xi's help, I got my app working again AND cleaned up the code by using PreviewView.
Reply all
Reply to author
Forward
0 new messages