Skip to content

Commit a5dc679

Browse files
committed
Implement Photo Picker in app module
1 parent 7cd5ebc commit a5dc679

4 files changed

Lines changed: 94 additions & 20 deletions

File tree

app/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
2. [DownloadManager](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/download/FileDownloaderImp.kt): is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. [Official](https://developer.android.com/reference/android/app/DownloadManager)
55
3. [Epoxy](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/epoxy/EpoxyActivity.kt): is an Android library for building complex screens in a RecyclerView. [Official](https://github.com/airbnb/epoxy)
66
4. [Pass data between components](https://github.com/AsemLab/Samples/tree/main/app/src/main/java/com/asemlab/samples/passdata): demonstrate how to pass data from Activity to Activity/Fragment and from Fragment to Activity/Fragment.
7-
5. [Progress in Notifications](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/NotificationProgressActivity.kt): display and update progressBar in notification.
8-
6. [RegisterForActifityResult](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): call an activity and retrieve the result from it. [Official](https://developer.android.com/training/basics/intents/result)
7+
5. [Photo Picker](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): provides a browsable interface that presents the user with their media library, sorted by date from newest to oldest. [Official](https://developer.android.com/training/data-storage/shared/photopicker)
8+
6. [Progress in Notifications](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/NotificationProgressActivity.kt): display and update progressBar in notification.
9+
7. [RegisterForActivityResult](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): call an activity and retrieve the result from it. [Official](https://developer.android.com/training/basics/intents/result)

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@
6363
<category android:name="android.intent.category.LAUNCHER" />
6464
</intent-filter>
6565
</activity>
66-
<activity android:name=".ui.RegisterForResultActivity" />
66+
<activity
67+
android:name=".ui.RegisterForResultActivity"
68+
android:exported="true" />
6769
<activity
6870
android:name=".ui.NotificationProgressActivity"
6971
android:exported="true">
70-
<!-- <intent-filter>-->
71-
<!-- <action android:name="android.intent.action.MAIN" />-->
72+
<!-- <intent-filter>-->
73+
<!-- <action android:name="android.intent.action.MAIN" />-->
7274

73-
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
74-
<!-- </intent-filter>-->
75+
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
76+
<!-- </intent-filter>-->
7577
</activity>
7678

7779
<receiver

app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.provider.ContactsContract
1010
import android.provider.MediaStore
1111
import android.util.Log
1212
import android.widget.Toast
13+
import androidx.activity.result.PickVisualMediaRequest
1314
import androidx.activity.result.contract.ActivityResultContracts
1415
import androidx.annotation.RequiresApi
1516
import androidx.appcompat.app.AppCompatActivity
@@ -78,26 +79,65 @@ class RegisterForResultActivity : AppCompatActivity() {
7879
}
7980
}
8081

82+
private val photoPickerLauncher =
83+
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) {
84+
if (it != null) {
85+
Toast.makeText(this, "Selected URI: $it", Toast.LENGTH_SHORT).show()
86+
} else {
87+
Toast.makeText(this, "No media selected", Toast.LENGTH_SHORT).show()
88+
}
89+
}
90+
91+
// TODO Pick multiple images/videos
92+
private val pickMultipleMedia =
93+
registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(5)) { uris ->
94+
if (uris.isNotEmpty()) {
95+
Toast.makeText(this, "Number of items selected: ${uris.size}", Toast.LENGTH_SHORT)
96+
.show()
97+
} else {
98+
Toast.makeText(this, "No media selected", Toast.LENGTH_SHORT).show()
99+
}
100+
}
101+
81102

82103
@SuppressLint("Range")
83104
override fun onCreate(savedInstanceState: Bundle?) {
84105
super.onCreate(savedInstanceState)
85106
binding = DataBindingUtil.setContentView(this, R.layout.activity_register_for_result)
86107

87-
binding.pickContact.setOnClickListener {
88-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
89-
if (contactPermissionNotGranted()) {
90-
permissionLauncher.launch(Manifest.permission.READ_CONTACTS)
108+
with(binding) {
109+
pickContact.setOnClickListener {
110+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
111+
if (contactPermissionNotGranted()) {
112+
permissionLauncher.launch(Manifest.permission.READ_CONTACTS)
113+
} else {
114+
getContactLauncher.launch(null)
115+
}
91116
} else {
92117
getContactLauncher.launch(null)
93118
}
94-
} else {
95-
getContactLauncher.launch(null)
96119
}
97-
}
98120

99-
binding.pickImage.setOnClickListener {
100-
imagePickerLauncher.launch("image/*")
121+
pickImage.setOnClickListener {
122+
imagePickerLauncher.launch("image/*")
123+
}
124+
125+
// TODO Use PhotoPicker
126+
newPickImage.setOnClickListener {
127+
photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
128+
// photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly))
129+
// photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))
130+
// Use with desired file type
131+
// photoPickerLauncher.launch(PickVisualMediaRequest(PickVisualMedia.SingleMimeType("image/gif)))
132+
}
133+
134+
pickVideo.setOnClickListener {
135+
photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly))
136+
}
137+
138+
pickMultiple.setOnClickListener {
139+
pickMultipleMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
140+
}
101141
}
102142

103143
}

app/src/main/res/layout/activity_register_for_result.xml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,55 @@
3838
android:layout_width="256dp"
3939
android:layout_height="wrap_content"
4040
android:text="Pick contact"
41-
android:textAllCaps="false"
41+
4242
app:layout_constraintBottom_toTopOf="@id/pickImage"
4343
app:layout_constraintEnd_toEndOf="parent"
4444
app:layout_constraintStart_toStartOf="parent"
4545
app:layout_constraintTop_toTopOf="parent"
4646
app:layout_constraintVertical_chainStyle="packed" />
4747

48-
4948
<Button
5049
android:id="@+id/pickImage"
5150
android:layout_width="0dp"
5251
android:layout_height="wrap_content"
5352
android:text="Pick image"
54-
android:textAllCaps="false"
55-
app:layout_constraintBottom_toBottomOf="parent"
53+
54+
app:layout_constraintBottom_toTopOf="@id/newPickImage"
5655
app:layout_constraintEnd_toEndOf="@+id/pickContact"
5756
app:layout_constraintStart_toStartOf="@+id/pickContact"
5857
app:layout_constraintTop_toBottomOf="@+id/pickContact" />
5958

59+
<Button
60+
android:id="@+id/newPickImage"
61+
android:layout_width="0dp"
62+
android:layout_height="wrap_content"
63+
android:text="New Pick image"
64+
65+
app:layout_constraintBottom_toTopOf="@id/pickVideo"
66+
app:layout_constraintEnd_toEndOf="@+id/pickContact"
67+
app:layout_constraintStart_toStartOf="@+id/pickContact"
68+
app:layout_constraintTop_toBottomOf="@+id/pickImage" />
69+
70+
<Button
71+
android:id="@+id/pickVideo"
72+
android:layout_width="0dp"
73+
android:layout_height="wrap_content"
74+
android:text="Pick video"
75+
76+
app:layout_constraintBottom_toBottomOf="parent"
77+
app:layout_constraintEnd_toEndOf="@+id/pickContact"
78+
app:layout_constraintStart_toStartOf="@+id/pickContact"
79+
app:layout_constraintTop_toBottomOf="@+id/newPickImage" />
80+
81+
<Button
82+
android:id="@+id/pickMultiple"
83+
android:layout_width="0dp"
84+
android:layout_height="wrap_content"
85+
android:text="Pick multiple"
86+
app:layout_constraintBottom_toBottomOf="parent"
87+
app:layout_constraintEnd_toEndOf="@+id/pickContact"
88+
app:layout_constraintStart_toStartOf="@+id/pickContact"
89+
app:layout_constraintTop_toBottomOf="@+id/newPickImage" />
90+
6091
</androidx.constraintlayout.widget.ConstraintLayout>
6192
</layout>

0 commit comments

Comments
 (0)