Commit bad02fad authored by Lucas NAURY's avatar Lucas NAURY

Ajout des images de la galerie et de l'appareil photo dans la page d'ajout

parent 7c54dcf2
package com.example.tpleboncoin.ui.ajout_annonce; package com.example.tpleboncoin.ui.ajout_annonce;
import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
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.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
...@@ -23,6 +33,8 @@ public class AjoutAnnonceFragment extends Fragment { ...@@ -23,6 +33,8 @@ public class AjoutAnnonceFragment extends Fragment {
private FragmentAjoutAnnonceBinding binding; private FragmentAjoutAnnonceBinding binding;
private ActivityResultLauncher<Intent> activityResultLauncher;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
AjoutAnnonceViewModel ajout_annonceViewModel = AjoutAnnonceViewModel ajout_annonceViewModel =
...@@ -59,6 +71,63 @@ public class AjoutAnnonceFragment extends Fragment { ...@@ -59,6 +71,63 @@ public class AjoutAnnonceFragment extends Fragment {
} }
}); });
final Button boutonAppareilPhoto = binding.photoBtn;
final Button boutonGalerie = binding.galerieBtn;
final ImageView imageAnnonce = binding.imageAnnonce;
boutonGalerie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
choisirImageDansGalerie();
}
});
boutonAppareilPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prendrePhoto();
}
});
activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
if(data != null) {
// On récupère l'URL de l'image
Uri selectedImageUri = data.getData();
if (selectedImageUri != null) {
// S'il y a bien une image, on l'affiche à l'écran
imageAnnonce.setImageURI(selectedImageUri);
imageAnnonce.setVisibility(View.VISIBLE);
} else {
// S'il n'y a pas de chemin, c'est que l'image est stockée en BMP
// dans les données du résultat (appareil photo)
Bitmap photo = (Bitmap) data.getExtras().get("data");
if (photo == null) {
return;
}
// S'il y a bien une photo, on l'affiche à l'écran
imageAnnonce.setImageBitmap(photo);
imageAnnonce.setVisibility(View.VISIBLE);
}
}
}
}
});
return root; return root;
} }
...@@ -67,4 +136,24 @@ public class AjoutAnnonceFragment extends Fragment { ...@@ -67,4 +136,24 @@ public class AjoutAnnonceFragment extends Fragment {
super.onDestroyView(); super.onDestroyView();
binding = null; binding = null;
} }
// Gestion des images
private void choisirImageDansGalerie(){
// Create an instance of the Intent of the type image
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
// Lancement de l'activité qui permet de sélectionner une image dans la galerie
activityResultLauncher.launch(Intent.createChooser(i, "Select Picture"));
}
private void prendrePhoto(){
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Lancement de l'activité qui permet de prendre une photo
activityResultLauncher.launch(Intent.createChooser(camera_intent, "Take Picture"));
}
} }
\ No newline at end of file
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
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">
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view" android:id="@+id/nav_view"
......
...@@ -28,11 +28,50 @@ ...@@ -28,11 +28,50 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="@dimen/activity_vertical_margin" /> android:minHeight="@dimen/activity_vertical_margin" />
<ImageButton <TextView
android:id="@+id/imageButton" android:id="@+id/titreImage"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_add_box_24" /> android:fontFamily="sans-serif-medium"
android:text="Image :"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<ImageView
android:id="@+id/imageAnnonce"
android:layout_width="match_parent"
android:layout_height="162dp"
android:visibility="gone"
tools:src="@tools:sample/backgrounds/scenic" />
<LinearLayout
android:id="@+id/imagesBtnContainer"
android:layout_width="match_parent"
android:layout_height="58dp"
android:orientation="horizontal">
<Button
android:id="@+id/photoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Photo"
app:icon="@drawable/ic_baseline_add_a_photo_24"
app:iconGravity="textStart" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/galerieBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Galerie"
app:icon="@drawable/ic_baseline_add_photo_alternate_24"
app:iconGravity="textStart" />
</LinearLayout>
<Space <Space
android:layout_width="match_parent" android:layout_width="match_parent"
......
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