From a5dc6797e4b5c222a471c70dcb50e29e1d997da9 Mon Sep 17 00:00:00 2001 From: Asem Abu Alrub Date: Mon, 30 Sep 2024 12:29:43 +0300 Subject: [PATCH] Implement Photo Picker in app module --- app/README.md | 5 +- app/src/main/AndroidManifest.xml | 12 ++-- .../samples/ui/RegisterForResultActivity.kt | 58 ++++++++++++++++--- .../layout/activity_register_for_result.xml | 39 +++++++++++-- 4 files changed, 94 insertions(+), 20 deletions(-) diff --git a/app/README.md b/app/README.md index 2922b5b..5566429 100644 --- a/app/README.md +++ b/app/README.md @@ -4,5 +4,6 @@ 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) 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) 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. -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. -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) +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) +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. +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) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c1a0df0..b346f1e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -63,15 +63,17 @@ - + - - + + - - + + + if (uris.isNotEmpty()) { + Toast.makeText(this, "Number of items selected: ${uris.size}", Toast.LENGTH_SHORT) + .show() + } else { + Toast.makeText(this, "No media selected", Toast.LENGTH_SHORT).show() + } + } + @SuppressLint("Range") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.activity_register_for_result) - binding.pickContact.setOnClickListener { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (contactPermissionNotGranted()) { - permissionLauncher.launch(Manifest.permission.READ_CONTACTS) + with(binding) { + pickContact.setOnClickListener { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (contactPermissionNotGranted()) { + permissionLauncher.launch(Manifest.permission.READ_CONTACTS) + } else { + getContactLauncher.launch(null) + } } else { getContactLauncher.launch(null) } - } else { - getContactLauncher.launch(null) } - } - binding.pickImage.setOnClickListener { - imagePickerLauncher.launch("image/*") + pickImage.setOnClickListener { + imagePickerLauncher.launch("image/*") + } + + // TODO Use PhotoPicker + newPickImage.setOnClickListener { + photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) +// photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly)) +// photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)) + // Use with desired file type +// photoPickerLauncher.launch(PickVisualMediaRequest(PickVisualMedia.SingleMimeType("image/gif))) + } + + pickVideo.setOnClickListener { + photoPickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly)) + } + + pickMultiple.setOnClickListener { + pickMultipleMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) + } } } diff --git a/app/src/main/res/layout/activity_register_for_result.xml b/app/src/main/res/layout/activity_register_for_result.xml index 1691d57..c70214a 100644 --- a/app/src/main/res/layout/activity_register_for_result.xml +++ b/app/src/main/res/layout/activity_register_for_result.xml @@ -38,24 +38,55 @@ android:layout_width="256dp" android:layout_height="wrap_content" android:text="Pick contact" - android:textAllCaps="false" + app:layout_constraintBottom_toTopOf="@id/pickImage" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_chainStyle="packed" /> -