產品新訊

內嵌相片挑選工具

8 分鐘閱讀

內嵌相片挑選工具:在應用程式中以更順暢的方式私下要求相片和影片

photopicker.png

準備好透過 Android 相片挑選工具的全新用法,提升應用程式的使用者體驗了嗎?使用者可透過全新的內嵌相片挑選工具,在應用程式介面中流暢地選取相片和影片,同時兼顧隱私權。現在應用程式也能享有相片挑選工具的所有優點,包括直接整合至應用程式體驗的雲端內容存取權。

為何要嵌入?

我們瞭解許多應用程式都希望在使用者選取相片或影片時,提供高度整合的流暢體驗。內嵌的相片挑選工具就是為此而生,可讓使用者快速存取最近的相片,不必離開應用程式。使用者也可以在偏好的雲端媒體供應商 (例如 Google 相簿) 中瀏覽完整媒體庫,包括我的最愛、相簿和搜尋功能。使用者不必切換應用程式,也不必擔心想找的相片是儲存在本機還是雲端。

完美整合,隱私權再升級

有了內嵌的相片挑選工具,應用程式不必存取使用者的相片或影片,直到使用者實際選取內容為止。這表示使用者可享有更高的隱私權,以及更流暢的體驗。此外,內嵌的相片挑選工具可讓使用者存取整個雲端媒體庫,而標準相片權限僅限於本機檔案。

Google 訊息內建的相片挑選工具

Google 訊息展示了內建相片挑選工具的強大功能。以下是整合方式:

  • 直覺式位置: 相片挑選工具位於相機按鈕正下方,讓使用者清楚選擇要拍攝新相片或選取現有相片。
  • 動態預覽:使用者輕觸相片後,系統會立即顯示大型預覽畫面,方便確認選取的相片。如果取消選取相片,預覽畫面就會消失,讓體驗保持簡潔。
  • 展開查看更多內容: 初始檢視畫面會簡化,方便你存取最近的相片。不過,使用者可以輕鬆展開相片挑選工具,瀏覽並選擇相片庫中的所有相片和影片,包括 Google 相簿中的雲端內容。
  • 尊重使用者選擇:內建的相片挑選工具只會授予使用者選取的特定相片或影片存取權,因此應用程式可以完全停止要求相片和影片權限。這樣一來,使用者只授予相片和影片的有限存取權時,訊息應用程式也不必處理相關情況。
gif1.gif
gif2.gif

導入

使用 Photo Picker Jetpack 程式庫,即可輕鬆整合內嵌相片挑選工具。  

Jetpack Compose

首先,請將 Jetpack 相片挑選工具程式庫做為依附元件納入。

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

您可以使用 EmbeddedPhotoPicker 可組合函式,直接在 Compose 畫面中加入內嵌相片挑選工具 UI。這個可組合函式會建立 SurfaceView,代管內嵌的相片挑選工具 UI。這個類別會管理與 EmbeddedPhotoPicker 服務的連線、處理使用者互動,並將所選媒體 URI 傳達給呼叫應用程式。  

@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

首先,請將 Jetpack 相片挑選工具程式庫做為依附元件納入。

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

如要新增內嵌相片挑選工具,請在版面配置檔案中新增項目。  

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

並在活動/片段中初始化。

// 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()
    )
}

您可以呼叫 EmbeddedPhotoPickerSession 的不同方法,與內嵌的挑選器互動。

// 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)

請注意,內嵌相片挑選工具體驗適用於搭載 Android 14 (API 級別 34) 以上版本,且 SDK Extensions 15 以上版本的使用者。進一步瞭解相片挑選工具的裝置適用情形

為加強保護使用者隱私權和安全性,系統會以防止任何繪圖或疊加的方式,算繪內嵌的相片挑選工具。這項刻意設計的選擇,代表您的 UX 應將相片挑選器的顯示區域視為獨立的專屬元素,就像您規劃廣告橫幅一樣。

如有任何意見回饋或建議,請向我們的問題追蹤器提交工單。

繼續閱讀