Skip to content

Commit

Permalink
Implement Photo Picker in app module
Browse files Browse the repository at this point in the history
  • Loading branch information
AsemLab committed Sep 30, 2024
1 parent 7cd5ebc commit a5dc679
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 20 deletions.
5 changes: 3 additions & 2 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 7 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.RegisterForResultActivity" />
<activity
android:name=".ui.RegisterForResultActivity"
android:exported="true" />
<activity
android:name=".ui.NotificationProgressActivity"
android:exported="true">
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>

<receiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.provider.ContactsContract
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -78,26 +79,65 @@ class RegisterForResultActivity : AppCompatActivity() {
}
}

private val photoPickerLauncher =
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) {
if (it != null) {
Toast.makeText(this, "Selected URI: $it", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "No media selected", Toast.LENGTH_SHORT).show()
}
}

// TODO Pick multiple images/videos
private val pickMultipleMedia =
registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(5)) { uris ->
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))
}
}

}
Expand Down
39 changes: 35 additions & 4 deletions app/src/main/res/layout/activity_register_for_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />


<Button
android:id="@+id/pickImage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Pick image"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintBottom_toTopOf="@id/newPickImage"
app:layout_constraintEnd_toEndOf="@+id/pickContact"
app:layout_constraintStart_toStartOf="@+id/pickContact"
app:layout_constraintTop_toBottomOf="@+id/pickContact" />

<Button
android:id="@+id/newPickImage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="New Pick image"

app:layout_constraintBottom_toTopOf="@id/pickVideo"
app:layout_constraintEnd_toEndOf="@+id/pickContact"
app:layout_constraintStart_toStartOf="@+id/pickContact"
app:layout_constraintTop_toBottomOf="@+id/pickImage" />

<Button
android:id="@+id/pickVideo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Pick video"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/pickContact"
app:layout_constraintStart_toStartOf="@+id/pickContact"
app:layout_constraintTop_toBottomOf="@+id/newPickImage" />

<Button
android:id="@+id/pickMultiple"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Pick multiple"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/pickContact"
app:layout_constraintStart_toStartOf="@+id/pickContact"
app:layout_constraintTop_toBottomOf="@+id/newPickImage" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit a5dc679

Please sign in to comment.