Novedades sobre productos

El selector de fotos incorporado

Lectura de 8 min

El Selector de fotos integrado: Una forma más fluida de solicitar fotos y videos de forma privada en tu app

photopicker.png

Prepárate para mejorar la experiencia del usuario de tu app con una nueva y emocionante forma de usar el selector de fotos de Android. El nuevo selector de fotos integrado ofrece una forma optimizada y centrada en la privacidad para que los usuarios seleccionen fotos y videos directamente en la interfaz de tu app. Ahora tu app puede obtener los mismos beneficios disponibles con el selector de fotos, incluido el acceso al contenido de la nube, integrado directamente en la experiencia de tu app.

¿Por qué se incorporó?

Entendemos que muchas apps quieren brindar una experiencia altamente integrada y fluida para los usuarios cuando seleccionan fotos o videos. El selector de fotos integrado está diseñado para hacer precisamente eso, ya que permite a los usuarios acceder rápidamente a sus fotos recientes sin salir de tu app. También pueden explorar su biblioteca completa en su proveedor de medios en la nube preferido (p.ej., Google Fotos), incluidas las funciones de favoritos, álbumes y búsqueda. Esto elimina la necesidad de que los usuarios cambien entre apps o se preocupen por si la foto que quieren está almacenada de forma local o en la nube.

Integración perfecta y privacidad mejorada

Con el selector de fotos integrado, tu app no necesita acceder a las fotos o los videos del usuario hasta que este seleccione algo. Esto significa mayor privacidad para tus usuarios y una experiencia más optimizada. Además, el selector de fotos integrado proporciona a los usuarios acceso a toda su biblioteca multimedia basada en la nube, mientras que el permiso de fotos estándar se restringe solo a los archivos locales.

El selector de fotos integrado en Mensajes de Google

Mensajes de Google muestra el poder del selector de fotos integrado. A continuación, te mostramos cómo lo integraron:

  • Ubicación intuitiva: El selector de fotos se encuentra justo debajo del botón de la cámara, lo que les brinda a los usuarios una opción clara entre capturar una foto nueva o seleccionar una existente.
  • Vista previa dinámica: Inmediatamente después de que un usuario presiona una foto, ve una vista previa grande, lo que facilita la confirmación de su selección. Si anulan la selección de la foto, la vista previa desaparece, lo que mantiene la experiencia limpia y ordenada.
  • Expandir para ver más contenido: La vista inicial se simplifica y ofrece acceso fácil a las fotos recientes. Sin embargo, los usuarios pueden expandir fácilmente el selector de fotos para explorar y elegir entre todas las fotos y videos de su biblioteca, incluido el contenido de la nube de Google Fotos.
  • Respeto por las elecciones del usuario: El selector de fotos integrado solo otorga acceso a las fotos o los videos específicos que selecciona el usuario, lo que significa que puede dejar de solicitar los permisos de fotos y videos por completo. Esto también evita que Mensajes tenga que controlar situaciones en las que los usuarios solo otorgan acceso limitado a fotos y videos.
gif1.gif
gif2.gif

Implementación

La integración del selector de fotos integrado es fácil con la biblioteca de Jetpack Photo Picker.  

Jetpack Compose

Primero, incluye la biblioteca de Jetpack Photo Picker como una dependencia.

implementation("androidx.photopicker:photopicker-compose:1.0.0-alpha01")

La función de componibilidad EmbeddedPhotoPicker proporciona un mecanismo para incluir la IU del selector de fotos integrado directamente en tu pantalla de Compose. Este elemento componible crea un SurfaceView que aloja la IU del selector de fotos integrado. Administra la conexión al servicio EmbeddedPhotoPicker, controla las interacciones del usuario y comunica los URIs de los elementos multimedia seleccionados a la aplicación que realiza la llamada.  

@Composable
fun EmbeddedPhotoPickerDemo() {
    // We keep track of the list of selected attachments
    var attachments by remember { mutableStateOf(emptyList<Uri>()) }

    val coroutineScope = rememberCoroutineScope()
    // We hide the bottom sheet by default but we show it when the user clicks on the button
    val scaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = rememberStandardBottomSheetState(
            initialValue = SheetValue.Hidden,
            skipHiddenState = false
        )
    )

    // Customize the embedded photo picker
    val photoPickerInfo = EmbeddedPhotoPickerFeatureInfo
        .Builder()
        // Set limit the selection to 5 items
        .setMaxSelectionLimit(5)
        // Order the items selection (each item will have an index visible in the photo picker)
        .setOrderedSelection(true)
        // Set the accent color (red in this case, otherwise it follows the device's accent color)
        .setAccentColor(0xFF0000)
        .build()

    // The embedded photo picker state will be stored in this variable
    val photoPickerState = rememberEmbeddedPhotoPickerState(
        onSelectionComplete = {
            coroutineScope.launch {
                // Hide the bottom sheet once the user has clicked on the done button inside the picker
                scaffoldState.bottomSheetState.hide()
            }
        },
        onUriPermissionGranted = {
            // We update our list of attachments with the new Uris granted
            attachments += it
        },
        onUriPermissionRevoked = {
            // We update our list of attachments with the Uris revoked
            attachments -= it
        }
    )

       SideEffect {
        val isExpanded = scaffoldState.bottomSheetState.targetValue == SheetValue.Expanded

        // We show/hide the embedded photo picker to match the bottom sheet state
        photoPickerState.setCurrentExpanded(isExpanded)
    }

    BottomSheetScaffold(
        topBar = {
            TopAppBar(title = { Text("Embedded Photo Picker demo") })
        },
        scaffoldState = scaffoldState,
        sheetPeekHeight = if (scaffoldState.bottomSheetState.isVisible) 400.dp else 0.dp,
        sheetContent = {
            Column(Modifier.fillMaxWidth()) {
                // We render the embedded photo picker inside the bottom sheet
                EmbeddedPhotoPicker(
                    state = photoPickerState,
                    embeddedPhotoPickerFeatureInfo = photoPickerInfo
                )
            }
        }
    ) { innerPadding ->
        Column(Modifier.padding(innerPadding).fillMaxSize().padding(horizontal = 16.dp)) {
            Button(onClick = {
                coroutineScope.launch {
                    // We expand the bottom sheet, which will trigger the embedded picker to be shown
                    scaffoldState.bottomSheetState.partialExpand()
                }
            }) {
                Text("Open photo picker")
            }
            LazyVerticalGrid(columns = GridCells.Adaptive(minSize = 64.dp)) {
                // We render the image using the Coil library
                itemsIndexed(attachments) { index, uri ->
                    AsyncImage(
                        model = uri,
                        contentDescription = "Image ${index + 1}",
                        contentScale = ContentScale.Crop,
                        modifier = Modifier.clickable {
                            coroutineScope.launch {
                                // When the user clicks on the media from the app's UI, we deselect it
                                // from the embedded photo picker by calling the method deselectUri
                                photoPickerState.deselectUri(uri)
                            }
                        }
                    )
                }
            }
        }
    }
}

Views

Primero, incluye la biblioteca de Jetpack Photo Picker como una dependencia.

implementation("androidx.photopicker:photopicker:1.0.0-alpha01")

Para agregar el selector de fotos integrado, debes agregar una entrada a tu archivo de diseño.  

<view class="androidx.photopicker.EmbeddedPhotoPickerView"
    android:id="@+id/photopicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Inicialízalo en tu actividad o fragmento.

// We keep track of the list of selected attachments
private val _attachments = MutableStateFlow(emptyList<Uri>())
val attachments = _attachments.asStateFlow()

private lateinit var picker: EmbeddedPhotoPickerView
private var openSession: EmbeddedPhotoPickerSession? = null

val pickerListener = object EmbeddedPhotoPickerStateChangeListener {
    override fun onSessionOpened (newSession: EmbeddedPhotoPickerSession) {
        openSession = newSession
    }

    override fun onSessionError (throwable: Throwable) {}

    override fun onUriPermissionGranted(uris: List<Uri>) {
        _attachments += uris
    }

    override fun onUriPermissionRevoked (uris: List<Uri>) {
        _attachments -= uris
    }

    override fun onSelectionComplete() {
        // Hide the embedded photo picker as the user is done with the photo/video selection
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_view)
    
    //
    // Add the embedded photo picker to a bottom sheet to allow the dragging to display the full photo library
    //

    picker = findViewById(R.id.photopicker)
    picker.addEmbeddedPhotoPickerStateChangeListener(pickerListener)
    picker.setEmbeddedPhotoPickerFeatureInfo(
        // Set a custom accent color
        EmbeddedPhotoPickerFeatureInfo.Builder().setAccentColor(0xFF0000).build()
    )
}

Puedes llamar a diferentes métodos de EmbeddedPhotoPickerSession para interactuar con el selector integrado.

// Notify the embedded picker of a configuration change
openSession.notifyConfigurationChanged(newConfig)

// Update the embedded picker to expand following a user interaction
openSession.notifyPhotoPickerExpanded(/* expanded: */ true)

// Resize the embedded picker
openSession.notifyResized(/* width: */ 512, /* height: */ 256)

// Show/hide the embedded picker (after a form has been submitted)
openSession.notifyVisibilityChanged(/* visible: */ false)

// Remove unselected media from the embedded picker after they have been
// unselected from the host app's UI
openSession.requestRevokeUriPermission(removedUris)

Es importante tener en cuenta que la experiencia del selector de fotos integrado está disponible para los usuarios que ejecutan Android 14 (nivel de API 34) o versiones posteriores con extensiones del SDK 15 o versiones posteriores. Obtén más información sobre la disponibilidad del selector de fotos en los dispositivos.

Para mejorar la privacidad y la seguridad del usuario, el sistema renderiza el selector de fotos integrado de manera que se evite cualquier dibujo o superposición. Esta elección de diseño intencional significa que tu UX debe considerar el área de visualización del selector de fotos como un elemento distinto y dedicado, de la misma manera en que planificarías un banner publicitario.

Si tienes comentarios o sugerencias, envía tickets a nuestro seguimiento de problemas.

Escrito por:

Seguir leyendo