Commit a7b30035 authored by m-spi's avatar m-spi

marche pas...

parent 6c2fff6d
...@@ -4,20 +4,19 @@ import android.app.Activity ...@@ -4,20 +4,19 @@ import android.app.Activity
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.ImageButton
import android.widget.ImageView import android.widget.ImageView
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.registerForActivityResult
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.elbuenopeso.databinding.FragmentAddBinding import com.example.elbuenopeso.databinding.FragmentAddBinding
...@@ -29,23 +28,42 @@ class AddFragment : Fragment() { ...@@ -29,23 +28,42 @@ class AddFragment : Fragment() {
// onDestroyView. // onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
var camera_open_id: Button? = null lateinit var camera_open_id: ImageButton
var gallery_open_id: Button? = null lateinit var click_image_id: ImageView
var click_image_id: ImageView? = null
private val galleryActivityResultLauncher: ActivityResultLauncher<Intent> = private val galleryActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult(), registerForActivityResult(ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback { fun onActivityResult( ActivityResultCallback { fun onActivityResult(
requestCode: Int, requestCode: Int,
resultCode: Int, resultCode: Int,
data: Intent? data: Intent
) { ) {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
val image = data?.data as Uri val image = data.data as Uri
click_image_id!!.setImageURI(image) click_image_id.setImageURI(image)
} }
}}
)
private var cameraActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback {
fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent
) {
Log.d("tag", "$resultCode")
if (resultCode == Activity.RESULT_OK) {
val image = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
data.extras?.getParcelable("data", Bitmap::class.java)
} else {
throw Exception("Oups, en fait c'est pas Android 14...")
}
camera_open_id.setImageBitmap(image)
binding.title.setText("aaa")
} }
} }}
) )
override fun onCreateView( override fun onCreateView(
...@@ -53,17 +71,25 @@ class AddFragment : Fragment() { ...@@ -53,17 +71,25 @@ class AddFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val notificationsViewModel = // val notificationsViewModel =
ViewModelProvider(this).get(AddViewModel::class.java) // ViewModelProvider(this)[AddViewModel::class.java]
_binding = FragmentAddBinding.inflate(inflater, container, false) _binding = FragmentAddBinding.inflate(inflater, container, false)
val root: View = binding.root val root: View = binding.root
click_image_id = binding.image
camera_open_id = binding.cameraButton
binding.image.setOnClickListener { binding.image.setOnClickListener {
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
galleryActivityResultLauncher.launch(gallery) galleryActivityResultLauncher.launch(gallery)
} }
binding.cameraButton.setOnClickListener {
val camera = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
galleryActivityResultLauncher.launch(camera)
}
// val textView: TextView = binding.textNotifications // val textView: TextView = binding.textNotifications
// notificationsViewModel.text.observe(viewLifecycleOwner) { // notificationsViewModel.text.observe(viewLifecycleOwner) {
// textView.text = it // textView.text = it
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
...@@ -28,6 +29,7 @@ ...@@ -28,6 +29,7 @@
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" /> app:navGraph="@navigation/mobile_navigation"
tools:ignore="FragmentTagUsage" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -15,7 +15,18 @@ ...@@ -15,7 +15,18 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_report_image" app:srcCompat="@android:drawable/ic_menu_report_image"
android:contentDescription="@string/todo" /> android:contentDescription="@string/gallery" />
<ImageButton
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:src="@android:drawable/ic_menu_camera"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image"
android:contentDescription="@string/camera" />
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/titleLayout" android:id="@+id/titleLayout"
...@@ -23,9 +34,9 @@ ...@@ -23,9 +34,9 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp" android:layout_marginHorizontal="20dp"
android:layout_marginVertical="20dp" android:layout_marginVertical="20dp"
android:hint="Title" android:hint="@string/title"
android:textColorHint="#6E6E6E" android:textColorHint="#6E6E6E"
app:layout_constraintTop_toBottomOf="@+id/image" app:layout_constraintTop_toBottomOf="@+id/cameraButton"
tools:layout_editor_absoluteX="20dp"> tools:layout_editor_absoluteX="20dp">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
......
...@@ -6,4 +6,7 @@ ...@@ -6,4 +6,7 @@
<string name="todo">TODO</string> <string name="todo">TODO</string>
<string name="address">Address</string> <string name="address">Address</string>
<string name="send">Send</string> <string name="send">Send</string>
<string name="camera">Camera</string>
<string name="title">Title</string>
<string name="gallery">Gallery</string>
</resources> </resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment