-
Notifications
You must be signed in to change notification settings - Fork 179
/
PhotoPicker.kt
173 lines (165 loc) · 7.24 KB
/
PhotoPicker.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.platform.storage.photopicker
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts.PickMultipleVisualMedia
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageAndVideo
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.ImageOnly
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.SingleMimeType
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.VideoOnly
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia.VisualMediaType
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.google.android.catalog.framework.annotations.Sample
/**
* This sample showcases how to select images/videos in a privacy-friendly way using the photo
* picker. Check out the AndroidManifest.xml of the storage section to see the <service> declaration
* that enables backport support on Android KitKat onwards using Google Play Services
*/
@OptIn(ExperimentalMaterial3Api::class)
@Sample(
name = "PhotoPicker",
description = "Select images/videos in a privacy-friendly way using the photo picker",
documentation = "https://developer.android.com/training/data-storage/shared/photopicker",
)
@Composable
fun PhotoPicker() {
var selectedMedia by remember { mutableStateOf(emptyList<Uri>()) }
// Keep track of the media type filter for the photo picker
var filter by remember { mutableStateOf<VisualMediaType>(ImageAndVideo) }
// Keep track of the maximum number of items we want to allow per selection
var maxItems by remember { mutableStateOf(5) }
/**
* [PickMultipleVisualMedia] is an ActivityResultContract that will launch the photo picker
* intent while providing an enhanced API over the available customisations options (media type,
* max number of items).
*
* If you're looking for single selection only, use PickVisualMedia ActivityResultContract
* instead.
*/
val pickMultipleMedia =
rememberLauncherForActivityResult(PickMultipleVisualMedia(maxItems)) { uris ->
selectedMedia = uris
}
Column(Modifier.fillMaxSize()) {
ListItem(
headlineContent = { Text("Filter") },
supportingContent = {
Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
PICKER_FILTERS.forEach { entry ->
FilterChip(
selected = filter == entry.value,
filter = entry.value },
label = { Text(entry.key) },
leadingIcon = if (filter == entry.value) {
{
Icon(
imageVector = Icons.Filled.Done,
contentDescription = "Selected option",
modifier = Modifier.size(FilterChipDefaults.IconSize),
)
}
} else {
null
},
)
}
}
},
)
Divider()
ListItem(
headlineContent = { Text("Max items") },
trailingContent = {
Button( pickMultipleMedia.launch(PickVisualMediaRequest(filter)) }) {
Text("Add media")
}
},
supportingContent = {
Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
MAX_ITEMS.forEach { value ->
FilterChip(
selected = maxItems == value,
maxItems = value },
label = { Text(value.toString()) },
leadingIcon = if (maxItems == value) {
{
Icon(
imageVector = Icons.Filled.Done,
contentDescription = "Selected option",
modifier = Modifier.size(FilterChipDefaults.IconSize),
)
}
} else {
null
},
)
}
}
},
)
LazyVerticalGrid(
modifier = Modifier.weight(1f),
columns = GridCells.Fixed(3),
horizontalArrangement = Arrangement.spacedBy(1.dp),
verticalArrangement = Arrangement.spacedBy(1.dp),
) {
items(selectedMedia) { uri ->
AsyncImage(
model = uri,
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.aspectRatio(1f),
)
}
}
}
}
private val PICKER_FILTERS = mapOf(
"All" to ImageAndVideo,
"Image" to ImageOnly,
"Video" to VideoOnly,
"GIF" to SingleMimeType("image/gif"),
)
private val MAX_ITEMS = listOf(2, 5, 10)